We're using Payara 4 which uses Eclipselink Moxy as the default JSON to/from object mapper.
We're now looking into switching to Jackson which is more suitable and easy to work with when it comes to JSON.
There is a known issue with Moxy where when it serializing a Map, the JSON will look like this:
"productsPerCost":{"entry":[{"key":"PS4","value":999}, {"key":"TV","value":1233}]}
And Jackson cannot deserialize this, and so it'll fail, because Jackson expect the Map's JSON representation to be:
"productsPerCost": {
"PS4": 999,
"TV": 1233
},
jackson with jaxb
Let's say we'll switch our application to work with Jackson. If we'll call to some external system that is working with Moxy, we'll get this weird JSON from them and fail.
How to handle this?
Related
After moving our application war from Glassfish3 to a deployment with Payara Micro, the JAX-RS serialization (jersey + jackson) doesn't work any more.
Thanks to Adam, we solved the issue with serializing pure collections, we now encounter similar errors when returning POJOs:
#GET
#Produces("application/json")
public BirdyTO findAllDaBirdy() {
return getBirdy();
}
where BirdyTO is a POJO which contains other POJOS and/or collections of POJOS.
That one gives us the error:
MessageBodyWriter not found for media type=application/json;charset=utf-8, type=class org.example.BirdyTO, genericType=class org.example.BirdyTO.
Strange thing is that similar interfaces in same application work fine.
Any idea?
Mapping of POJOs to JSON is not standardized in Java EE. Glassfih 4/Payara use MOXy to map POJO to JSON by default, which uses JAXB for the mapping. See [this post by Reza Rahman] (https://blogs.oracle.com/theaquarium/entry/moxy_is_the_new_default). It is possible that BirdyTO cannot be mapped by Moxy.
If you want to use Jackson, you have to:
disable default Moxy feature (by setting jersey.config.server.disableMoxyJson property to true)
add Jackson library into your app (com.fasterxml.jackson.jaxrs)
turn on the JacksonFeature (provided by the Jackson library) in your JAX-RS application
More info how to do it in this answer: Customizing JSON marhsalling with GlassFish v4
I use Jackson FasterXML product to deserialize JSONs. Now, I noticed in profiler that I got a ton of duplicate strings as a result since every time I receive a JSON it deserialize into an object which always contains some String variable which tells me the Type name. (it's answer to Facebook GraphQL query). Now, naturally, I would prefer for .intern() method to be used during deserialization of this specific method. Is that possible? If so, how?
Seems StringDeserializer can provide whttp://stackoverflow.com/questions/17041249/jackson-override-primitive-type-deserialization
I'm using Spring boot with springfox and having problems parsing JSON payload from Swagger UI into Java class (JSON->POJO). No errors, but the resulting Java object is missing a field (null). The top class for the class with missing attribute has Mixin to switch datatypes. That part works fine.
I can't really debug since framework does most of the parsing. The req-d attribute is displayed in Swagger under Model Schema correctly. However, when I submit JSON payload containing same attribute, the corresp POJO's attribute is null.
Please steer me to the right direction.
I've figured it out my issue: I was missing setter for the field in the Mixin interface. The JSON was being parsed, but the setter was missing, hence null value.
In our project we expose a number of web-services that were generated from a wsdl. After generating them, I can see that the requests and responses are mapped to POJOs and when I am making the response, I just set a new POJO. This works really nice. However, I have a problem with the request. When we receive the request I expected that the payload will be a POJO mapping the parameters from the request. The payload becomes actually an array of objects. I can access the values but this is not very comfortable. You can take a look at the picture.
I can see that the under "Variables" in the method it is correctly matched to the POJO we would like to have. Is there some setting that I am missing somewhere so that we can get the payload to be mapped to correct POJO type?
Re-run the WSDL to Java codegen but this time with wrapper style disabled, see: https://cxf.apache.org/docs/wsdl-to-java.html#WSDLtoJava-wrapperstyle
I noticed that Web API can return a DataTable as JSON but balks when returning it as XML.
What is (or are) the base interface(s) that your objects should inherit from in order for Web API to automagically serialize them?
And why would it be able to serialize a given object as JSON but not XML?
ASP.NET Web API uses DataContractSerializer for XML and JSON.NET for JSON. So, the respective serialization requirements hold good with web API as well. Check out this and this.
DataTable should get serialized by DCS, since DataTable implements IXmlSerializable. If you want more control over how XML is produced, you can implement IXmlSerializable yourself.