Search code examples
glassfishjax-rsjackson2payara-micro

JAX-RS: serializing a POJO fails on payara micro


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?


Solution

  • 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