Glassfish 4.1 : Replace default ( Moxy ) JSON-Binding Provider by Jackson 2.5.1

I switched from GlassFish 3.1.2 to GlassFish 4.1. for my project. I got some unexpected result while producing JSON since GlassFish 4.1 using Moxy as New Default JSON-Binding Provider instead of Jackson which is default for GlassFish 3.1.2. So I decided to use latest Jackson 2.5 for GlassFish 4.1 as default JSON-Binding Provider.

1.You need to register org.glassfish.jersey.jackson.JacksonFeature in your application if you want to use Jackson as your JSON provider. (by registering this, MOXy will be disabled).
Please check below code for registering JacksonFeature.

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.jackson.JacksonFeature;

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

	private Logger LOGGER = Logger.getLogger(this.getClass().getName());
	
	@Override 
	public Set<Class<?>> getClasses() 
	{ 
		final Set<Class<?>> classes = new HashSet<>(); 
		// register root resource 
		classes.add(MyService.class); 
                
                // register JacksonFeature so that glassfish will use jackson instead of moxy.
		try
		{
		        classes.add(JacksonFeature.class);
		}catch(Exception e)
		{
			LOGGER.log(Level.SEVERE,"MyApplication : Jackson library not added.",e);
		}
		return classes; 
		
	} 
}

2. Add below jars in GlassFish’s module folder i.e “HOME/glassfish4/glassfish/modules”
– jackson-annotations-2.5.1.jar
– jackson-core-2.5.1.jar
– jackson-databind-2.5.1.jar
– jackson-module-jaxb-annotations-2.5.1.jar
– jackson-jaxrs-base-2.5.1.jar
– jackson-jaxrs-json-provider-2.5.1.jar

3. Don’t forget to restart server 🙂 Now your GlassFish 4.1 is ready to use jackson.

Happy Coding 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s