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