I need to write a Json client in Android for Zenfolio API. I decided to use Spring ResTemplate with MappingHttpJacksonConverter. When i do POST with "exchange" method i recieve json response with one element named "#type" that causes deserializatoon exception. Is there an annotation that tells deserializer to omit that tag? How to turn on annotation for json deserializer?
Try #JsonIgnoreProperties(ignoreUnknown=true) on your mapping classes if you want to ignore all elements that you are not interested in.
Try using #JsonIgnore ("#type"), Jackson annotations are enabled by default.
See http://wiki.fasterxml.com/JacksonAnnotations for more info.
Related
I was working with rest api and I have a json on POST which I need to map to a dto. But, I have only 5 properties on json , but more than that on the dto. How do I use bean mapper to map it automatically and what about the rest of the properties. Will they be set to Null?
Spring boot comes with Jackson deserializer out-of-the-box. So, it will use the proper method (default null value or constructor properties, based on your settings). For fine tuner, see more at JsonInclude annotation for collections include's strategy and so.
JsonInclude
Using jersey jersey.java.net How do I set JSON as the default serialization instead of XML when there is no accept header or .xml suffix is in the URI?
You can assign the quality index to each media type in #Produces annotation. I.e.you can do the following to make Jersey prefer JSON if both XML and JSON are allowed:
#Produces({"application/json;qs=1", "application/xml;qs=.5"})
You should be able to set the #Produces annotation to specify the return format like so:
#Produces( { "application/json" })
How come there is no accepts header?
You can specify preference of generation by specifying media types in your order of preference in the #Produces annotation.
#Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
In the above code since "application/json" comes first, if no accept header is specified in the request Jersey will default to generating JSON response.
Using qs (as suggested by Martin) makes the preference more explicit, but its a bit more complicated to understand.
Can someone please help clarify and show how to properly go about this issue?
What I understand so far is that the Spring framework uses Message Converters when a method is annotated with #ResponseBody to convert the return Java object to a format that can be accepted by the Client. If the clients' HTTP request Accept Header includes "application/json", it will use Jackson and the Jackson converter to convert the object and return it in a json format. Similarly if the Accept Header includes "application/xml", then the Message Converter will use Jaxb and the corresponding converter to convert the object to xml.
Now my issue is that I include both the Jackson and Jaxb libraries as specified in Spring documentation so that the corresponding converters can work. This should be enough for Spring to employ #ResponseBody as its supposed to. However, when I send an HTTP Request with the Accept header "application/xml" I get a 406 status code and when I send one with "application/json" I receive a correct json response.
From my research online, I see that some people use the ContentNegotiation technique to work around this, but I would like to use the Message Converter for now. However, every technique to make the Message Converter technique to respond to json and xml resource requests involve formatting my POJO with JAXB annotation. Is this really necessary?
I guess what I am asking is how would one set up their project properly so that Spring can use the Message Converter technique to respond to json and xml requests? What libraries must be included? Does one need to add JAXB annotations or is there an automatic way for Spring to format an object into xml the way it does for json?
I thank you for your time and help with this, but so far I am really loving Spring's implementation of JAX-RS!
I have a requirement to create a REST service(Jersey) which accepts header as "application/com.foo+xml" (+json incase of JSON mime type).
Is there anyway to have
#Produces("application/com.foo+xml")
without creating a custom MessageBodyWriter? Is there anyway to map "application/com.foo+xml" to "application/json"?
Just don't want to create a custom class when MediaType "application/com.foo+xml" is same as "application/xml"
I imagine you are using JAXB for handling your API messages (request/response) - if not you should look on that. Apparently what you are looking for is possible without creating custom MessageBodyWriter, according to this reference - http://jersey.576304.n2.nabble.com/Application-Specific-content-types-and-JAXB-annotations-td6380235.html - "Anything "+json" should work out of the box"... so you would simply need to define your JAXB mappings and it will generate/handle the JSON/XML representations for the #Consumes and #Produces MediaTypes you have on your API.
We have a Resteasy webservice.
I use Jackson provider for JSON, both outgoing JSON in response and incoming JSON in request.
Is it possible to have a PostProcessInterceptor to be executed after JSON-Jackson serialization?
My PostProcessInterceptor has to change the JSON content for every outgoing response. But when the PostProcessInterceptor is executed if I print the entity response.getEntity().toString(); I see the toString method of the java.lang.Object, not the JSON String. That's because the Object has not yet been serialized by Jackson.
Is it possible to serialize with Resteasy/Jackson before running the PostProcessInterceptor?
I've also tried to use #Precedence annotation on my PostProcessInterceptor. But it doesn't work, even using "DECODER" precedence (which is the last one).
Any idea? Thanks in advance.
I would go for a CDI interceptor instead. You can get the intercepted method parameters from the InvocationContext and change them if necessary.