I am looking to get the key and value to each Json formatted call and use them as java objects such as String or Integer ,in a rest client i would enter { "Name":"HelloWorld" }
And i would get back the HelloWorld mapped to its Key so far ive seen examples but im just having trouble finding out what each tag does and how to parse the body to give the above results
@POST
@Path("/SetFeeds")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@JsonCreator
public String setFeed(String jsonBody,@Context UriInfo uriInfo){
...Code to manipulate the body of the request
return response;
}
First thing you need to understand is how request body parsing is done. In JAX-RS parsing (or unmarshalling/deserializing/whatever) is done with
MessageBodyReader
s. There are different readers that can handle different Content-Type. For instance if you have Content-Typeapplication/octet-stream
, there is a reader that will unmarshal tobyte[]
orFile
orInputStream
. So the following would work out the boxThat being said, JAX-RS implementations come with very basic readers for "easily convertible" format. For example, most requests can be converted to
String
, so you get that free for most Content-Types, as you are with your current code.If we want some more complex data types, like your
HelloWorld
for Content-Typeapplication/json
, there is no standard reader for this. For this to work, we either need to create our own reader, or use a library that comes with a reader. Luckily, the most popular JSON framework in Java, Jackson, has implemented a JAX-RS provider that has a reader and a writer (for serialization).Now depending on what server/JAX-RS implementation you are using, different implementations create light wrappers around the core Jackson JAX-RS module. If I knew the JAX-RS implementation you were using, I could recommend which wrapper to use, or you can forget the wrapper and just go with the basic Jackson module, which is
The above is a Maven dependency. If you are not using Maven, then basically you need to download all these jars.
You can find all of them here. Just search for them individually.
Then you need to register the provider. Again it depends on your JAX-RS implementation and how you are handling the configuration of your resource classes. I would need to see your application configuration (either web.xml or Java code) and maybe the server you are using to help with that. For the most part the
JacksonJsonProvider
(which is the reader and writer) needs to be registered.Once you have it registered then you need to understand the basics of how Jackson handle the serializaion. At most basic level, Jackson looks for JavaBean properties (basic getter/setter) to match with JSON properties. For instance if you have this bean property
The JSON should look like
{"name": "whatever"}
. The"name"
key is the same as the bean property. In Bean property terms, the name of the property is all letters after theget/set
with the first letter lowercased.That's pretty much all there is to it. Now you can do
For more complex JSON formats, you should refer to the Jackson documentation or ask a question here on SO.
As far as the registering of the
JacksonJsonProvider
, if you are having trouble, please provide the information I requested, i.e. application configuration (web.xml or Java config) and the server you are using.See Also: