How to handle JSON payload using reset wizard?

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 query will 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 handle a 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?
}
+4
source share
1 answer

Check out Jackson .

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

As long as usernameJSON is valid and maps it to Thing, this should work.

. , Thing. , , com.fasterxml.jackson.annotation.JsonManagedReference. API.

+4

All Articles