Skip to content

Instantly share code, notes, and snippets.

@chongma
Last active February 12, 2020 16:53
Show Gist options
  • Select an option

  • Save chongma/60c7f308bf4896fea9155d710ee81d5f to your computer and use it in GitHub Desktop.

Select an option

Save chongma/60c7f308bf4896fea9155d710ee81d5f to your computer and use it in GitHub Desktop.
package com.test;
import java.io.InputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import org.apache.johnzon.mapper.Mapper;
import org.apache.johnzon.mapper.MapperBuilder;
@Path("/test")
public class TestService {
@Path("/myObjects/{id}/{type}")
@PUT
@Consumes("application/json")
public Response test(@PathParam("id") Long id, @PathParam("type") String type,
InputStream inputStream) {
System.out.println(type);
final Mapper mapper = new MapperBuilder().build();
final String otherObject = mapper.readObject(inputStream, String.class);
System.out.println(otherObject);
return Response.ok().build();
}
}
@rmannibucau
Copy link

rmannibucau commented Feb 12, 2020

// curl -X PUT -H 'Content-Type: application/json' http://localhost:8080/test/myObjects/1/foo -d '{\"foo\":\"bar\"}' -v
@Path("test")
@ApplicationScoped
public class TestService {
    private Jsonb jsonb;

    @PostConstruct
    private void init() {
        jsonb = JsonbBuilder.create();
    }

    @PreDestroy
    private void destroy() {
        try {
            jsonb.close();
        } catch (final Exception e) {
            // no-op, not important here
        }
    }

    @PUT
    @Path("myObjects/{id}/{type}")
    @Consumes("application/json")
    public Response test(@PathParam("id") Long id, @PathParam("type") String type,
                         InputStream inputStream) {
        final JsonObject object = jsonb.fromJson(inputStream, JsonObject.class);
        final String otherObject = object.toString();
        System.out.println(otherObject);
        return Response.ok(object).build();
    }

    public static void main(final String... args) {
        try (final Meecrowave meecrowave = new Meecrowave()) {
            meecrowave.bake().await();
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment