mikeski.net kotlin java javascript hugo development

mikeski.net development blog

Configure Jackson for Glassfish 4.1


I Needed to configure Jackson’s JSON library for Glassfish 4.1 and was getting errors due to another library (MOXY) being used. This article is about how to fix the Jackson error.

First, we implement the javax.ws.rs.core.Feature interface:

Setup the JacksonFeature

public class JacksonFeature implements Feature {
    Logger L = LoggerFactory.getLogger(JacksonFeature.class);
    public boolean configure(final FeatureContext context) {
        String postfix = "";
        postfix = '.' + context.getConfiguration().getRuntimeType().name().toLowerCase();
        context.property(CommonProperties.MOXY_JSON_FEATURE_DISABLE + postfix, true); 
        L.info("Set property to true disable MOXY: " + CommonProperties.MOXY_JSON_FEATURE_DISABLE + postfix);
        context.register(JacksonJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
        return true;

Service configuration

Next, we configure our web service by extending the ResourceConfig class provided by Glassfish.

public classMyResourceConfig extends ResourceConfig {
    publicMyServicesResourceConfig() {
        register( new GZipEncoder() );
        register( JacksonFeature.class );
    private void addMyResources() {
        register( Service1.class );
        register( Service2.class );

There is also a package(‘net.my.package’) to register a bunch of services.


Here we can see some test code:

    public void testJF() {
        // name().toLowerCase()
        RuntimeType rt = RuntimeType.SERVER;
        // getRuntimeType()
        Configuration c = mock(Configuration.class);
        // context.getConfiguration()
        FeatureContext fc = mock(FeatureContext.class);
        try {
            jf = new JacksonFeature();
        } catch (Exception e) {
            fail("Caught exception: " + e.getMessage());