How to process a JSON payload with drop wizard?

dropwizard, java, jersey, json

Solution

Check out Jackson.

ObjectMapper mapper = new ObjectMapper();
Thing impl = mapper.readValue(username, Thing.class);

As long as `username` is valid JSON and maps to `Thing`, this should work.

Note: You may have to annotate the class members or methods of `Thing`. For example in my case I had to manage circular references using `com.fasterxml.jackson.annotation.JsonManagedReference`. But you can read up about this and other annotations in the API documentation.

Problem

I have a simple class called `Thing` ``` public class Thing { private int id private String name //getters, setters, constructor } ``` I would like to send a request with a payload and process it. The request would look something like this: ``` curl -i -d '{"thing": {"id": 11, "name": "foobar"}}' http://localhost:8080/thing/{username} ``` But I can't figure out how to process the json request. This is what my method looks like: ``` @Path("/thing/{username}") @POST public Thing add(@PathParam("username") String username) { //how can I process the JSON payload sent and convert it to Thing object? } ```

Original source