Ralph
Ralph

Reputation: 4868

Why have Rest-Easy and Jersey different JSON Output Formats?

I have written a rest service API which returns a data structure in a kind of a map layout. The map entries can be from type String, Integer or Date. The rest service method supports XML and Json output.

Now I recognized that the JSON outcome in GlassFish (Jersey) is different as in Wildfly (Reas-Easy)

When running the rest servie on GlassFish with application/json the output looks like this:

{"entity":{"item":{"name":"$modified","value":{"@type":"xs:dateTime","$":"2015-02-17T22:33:57.634+01:00"}}}}

And the same result on WildFly (Rest-Easy) looks like this:

{"entity":[{"item":[{"name":"$modified","value":[1425822673120]}]}]}

Can anybody explain this behavior? I would expect that the output in WildFly should be simmilar to GlassFish?

The interesting thing is that When I call the same method with the request header 'application/xml' both systems return the same (expected) format.

GlassFish XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<entity><item><name>$modified</name>
<value xsi:type="xs:dateTime">2015-02-17T22:33:57.634+01:00</value></item></entity></collection>

Wildfly XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<entity><item><name>$modified</name>
<value xsi:type="xs:dateTime">2015-03-08T14:51:13.120+01:00</value></item></entity></collection>

Is there a way to configure the JSON Format for Rest-Easy?

Upvotes: 3

Views: 1887

Answers (2)

Paul Samsotha
Paul Samsotha

Reputation: 209052

It would help if we had the model classes to test, so we could see which providers produce which result (while testing). But without it, I'll just throw some things to consider

Glassfish by default uses MOXy for JSON/POJO support. I personally do not like using MOXy. At first I promoted it's usage, as it's recommended by Jersey, but after a while, you start to learn its limitations. Glassfish also ships with Jackson support, but we need to either disable MOXy explicitly, or just register a Jackson Feature (which is not portable) or a combination of diabling MOXy and adding a Jackson provider.

As far as Wildfly, one thing to consider as mentioned here in the Resteasy Documentation

21.6. Possible Conflict With JAXB Provider

If your Jackson classes are annotated with JAXB annotations and you have the resteasy-jaxb-provider [which Wildfly comes shipped with] in your classpath, you may trigger the Jettision JAXB marshalling code. To turn off the JAXB json marshaller use the @org.jboss.resteasy.annotations.providers.jaxb.IgnoreMediaTypes("application/*+json") on your classes.

Another thing to consider is that both servers come shipped with providers for both Jackson 1.x and Jackson 2.x. Which ever one used may not have a difference in the marshalling result, but is relevant to the next part of this answer (also see here - though this mentions JBoss AS7, I'm not sure if it applies to Wildfly. I think Wildfly uses Jackson 2 by default though).

One way to test which provider is being used is to create a ContextResolver. Now this next example is simply for testing purposes ( you wouldn't normally just add Jackson by itself, but the Jackson provider).

Add this dependency to your project

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.0</version>
</dependency>

Add this class

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

@Provider
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper>{

    static final Logger logger 
                 = Logger.getLogger(ObjectMapperContextResolver.class.getName());
    final ObjectMapper mapper = new ObjectMapper();

    @Override
    public ObjectMapper getContext(Class<?> type) {
        logger.log(Level.INFO, "<===== ***** Jackon 2 is used ***** =====>");
        return mapper;
    } 
}

Result

Glassfish: Jackson 2 not being used
Wildfly: Jackson 2 is used (even with JAXB annotations. Maybe you need to explicitly have the resteasy-jaxb-provider explicitly on the project classpath for the Jettison to kick in).

So how can we fix Glassfish deployment, in a portable way? One way I was able to test and get Jackson 2 to be used on both servers, is to disable MOXy, by adding a server configuration property. This is portable as the property is nothing more than a string. It will be ignored by Resteasy.

@ApplicationPath("/rest")
public class AppConfig extends Application {

    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("jersey.config.disableMoxyJson", true);
        return properties;
    }
}

We'll also need to add the Jackson provider to the project

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>2.4.0</version>
</dependency>

Odd that we have to add this dependency, as Glassfish already comes shipped with it, but if I don't add it, I'll get a MessageBodyWiter not found.

This solution has been tested on Wildfly 8.1 and Glassfish 4.0

Upvotes: 4

lefloh
lefloh

Reputation: 10961

JAX-RS specifies the interface MessageBodyWriter for serializing your entities but this defines only that a Java Object is converted to an OutputStream. How the result looks like is up to you or the JAX-RS runtime.

Both, RESTeasy and Jersey ship with serializers for MediaTypes like application/json or application/xml and a default serializer which is used if no better one is found.

For serializing a Java Object to XML there is JAXB as standard so the result should not differ (too much). For JSON the situation is different, there is no explicit standard but a lot of serializers like e.g:

They all handle some things differently can be configured differently and changed behavior during their history.

To answer the last question: If you are using RESTeasy on Wildfly you are usually using Jackson and you can configure a lot. Here's an example.

Beware that the Jackson configuration options have been renamed from time to time.

Upvotes: 3

Related Questions