3

I've seen many articles and SO-questions about this - but I just don't get it working. My goal is to use Jackson as JSON processor in a JavaEE application. What do I have so far?

pom.xml

  • either this one

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.17</version>
    </dependency>
    
  • or this one (which of them is correct in the end?)

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.5.2</version>
    </dependency>
    
  • plus this (due to this article, because auto discovery shall not exist in jackson packages anymore):

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-metainf-services</artifactId>
        <version>2.17</version>
    </dependency>
    

web.xml

Simple REST registration:

<servlet>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>javax.ws.rs.core.Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

A simple Object

import com.fasterxml.jackson.annotation.JsonProperty;

public class Dummy {
    private String name;

    @JsonProperty("username")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

The REST resource

@GET
@Path("test")
@Produces(MediaType.APPLICATION_JSON)
public Response getRequest() {
    Dummy dummy = new Dummy();
    dummy.setName("rolf");

    return Response.ok(dummy).build();
}

And the output is

{"name":"rolf"}

instead of the expected

{"username":"rolf"}

Update

I'm using the GlassFish application server.

thomas.mc.work
  • 6,404
  • 2
  • 26
  • 41
  • Be careful about the JsonProperty annotation you are actually using, [this answer](http://stackoverflow.com/questions/22173418/jackson-json-deserialisation-unrecognized-field-not-marked-as-ignorable#answer-26063611) mentions, two options for the package coordinates. – superbob Apr 17 '15 at 12:01
  • @superbob That's totally correct. But curiously the change is performed from new to old package. There are many confusing articles and SO-questions with the outdated version pre 2.0 of Jackson. I've updated the example. – thomas.mc.work Apr 17 '15 at 12:05
  • Have you tried playing with [ContextResolver](http://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ContextResolver.html) ? This question seems interesting: http://stackoverflow.com/questions/5005668/configure-jersey-jackson-to-not-use-xmlelement-field-annotation-for-json-field/5008595#5008595 – superbob Apr 17 '15 at 12:29
  • It seems that depending on the underlying JAX-RS implementation, you may need to register the JSON mapper, see https://jersey.java.net/nonav/documentation/latest/user-guide.html#mig-1-x-json for example for jersey – superbob Apr 17 '15 at 12:47

1 Answers1

3

My guess would be you're on Glassfish, which uses MOXy as its default JSON provider. You can disable it with an <init-param>.

<init-param>
    <param-name>jersey.config.server.disableMoxyJson</param-name>
    <param-value>true</param-value>
</init-param>

The jersey-media-json-jackson has an auto-discoverable feature that should automatically register it. I'm not sure about the auto-discoverable feature in the case of Glassfish, and possible lower version of Jersey it uses internally, and if it will cause it not to be register. But either way, the way you have configured you web.xml is to enable classpath scanning, so the Jackson provider should be picked up anyway.

Some FYIs

  • jersey-media-json-jackson actually uses jackson-jaxrs-json-provider. It just wraps it in a JacksonFeature, and enables auto-discovery of it.
  • If it still doesn't work, you can try to create a feature to handle the registering and disabling. For example

    @Provider
    public class JsonFeature implements Feature {
        @Override
        public boolean configure(FeatureContext context) {
            context.property("jersey.config.server.disableMoxyJson", true);
            // this is in jersey-media-json-jackson
            context.register(JacksonFeature.class);
    
            // or from jackson-jaxrs-json-provider
            context.register(JacksonJsonProvider.class);
            // for JAXB annotation support
            context.register(JacksonJaxbJsonProvider.class);
    
            return true;
        }
    }
    
thomas.mc.work
  • 6,404
  • 2
  • 26
  • 41
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • You're right, it's GlassFish. I was hoping that all this can be handled indepently of the Application server, and thus also independently of the target JAXB implementation. – thomas.mc.work Apr 17 '15 at 20:36
  • Use the String property. That will not depend on a implementation. Using another implementation, the property will simply be ignored. If you want not use the `jersey-media-json-jackson`, as that is Jersey specific, then if for some reason the Jackson provider from `jackson-jaxrs-json-provider` is not picked up from the classpath scanning, you can register it in the `Feature` above. `register(JacksonJsonJaxbProvider.class)`. You can look in the jar, there are other classes like an ExceptionMapper you might want to register also. But this should be picked up by the classpath scanning anyway – Paul Samsotha Apr 17 '15 at 20:40
  • And I don't know what you mean by JAXB implemention. This problem has nothing to do with JAXB – Paul Samsotha Apr 17 '15 at 20:41
  • Ok, that was misleading. I definitely need to name the Jackson library (which I think is used by Jersey as the JAXB implementation for de-/serialization of objects, isn't it?), but I don't like to name the Jersey library, because it's only used in GlassFish. My favorite goal is to have a solution which works in other application servers too. – thomas.mc.work Apr 21 '15 at 08:44
  • Strange conclusion: Now it works with the setup from my question, except for dropping the `jackson-jaxrs-json-provider` dependency. Anyway, thank you for your reply which is still very helpful! On the basis of this I have [another question](http://stackoverflow.com/questions/29767579/register-jackson-mixin-in-javaee-application) which will finally solve my whole problem. – thomas.mc.work Apr 21 '15 at 09:05