Use Jackson as JAXB-JSON Processor in JavaEE Application

I've seen many articles and SO questions about this, but I just don't work. My goal is to use Jackson as JSON processor in JavaEE application. What do I still have?

pom.xml

  • either this

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.17</version>
    </dependency>
    
          

  • or this one (which one is correct at 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 , as auto-discovery will no longer exist in jackson packages):

    <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>

      

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;
    }
}

      

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 conclusion

{"name":"rolf"}

      

instead of expected

{"username":"rolf"}

      

Update

I am using GlassFish Application Server.

+3


source to share


1 answer


I'm assuming you're on Glassfish, which uses MOXy as its default JSON provider. You can turn it off with <init-param>

.

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

      

jersey-media-json-jackson

has an auto detection function that should automatically register it. I'm not sure about the auto-detection capability in the case of Glassfish and the possible underside version of Jersey it uses internally and if that would result in it not being registered. But anyway, the way you set up your web.xml class scanning needs to be enabled, so the Jackson provider should be selected anyway.



Some FYI

  • jersey-media-json-jackson

    uses jackson-jaxrs-json-provider

    . It just wraps it in JacksonFeature

    and lets you open it automatically.
  • If it still doesn't work, you can try to create a function to handle registration and disconnection. 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;
        }
    }
    
          

+1


source







All Articles