简体   繁体   中英

How to add JSON Support in Jersey 2.x

I have migrated my application from Jersey 1.x to 2.x and I am trying to replace the following entry from the web.xml

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>

in order to be able to get entities as json.

I have read in the official documentation that I have to use the Jackson provider by adding jersey-media-json-jackson dependency and thne register the JacksonFeature.

I added the following dependency:

<dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.1</version>
</dependency>

but now I don't know what I have to add in my web.xml file as a value in the

<param-name>jersey.config.server.provider.packages</param-name>

Any ideas ? I wan't to do it via a configuration file like web.xml and not inside my code.

My configuration looks like this:

    <servlet>
        <servlet-name>REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.<your-company-name-here></param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

I register everything using a ResourceConfig class. I also found a resource that describes how to do it other ways ( http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/ ).

@ApplicationPath("/ws")
public class WsResourceConfig extends ResourceConfig {

    public WsResourceConfig() {
        register(Jackson2Feature.class);
        register(ObjectMapperResolver.class);
        //register(MultiPartFeature.class); // if needed
        //register(new LoggingFilter(LOGGER, true)); // to enable logging

        //register(my resources...); to register my service classes
    }

    private static class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
        private static ObjectMapper mapper = new ObjectMapper();
        static {
            // configure mapper here
        }
        @Override
        public ObjectMapper getContext(Class<?> type) {
            return mapper;
        }
    }

    /**
     * Feature to disable Moxy and Enable Jackson processing
     */
    private static class Jackson2Feature implements Feature {

        @Override
        public boolean configure(FeatureContext context) {
            final String disableMoxy = PropertiesHelper.getPropertyNameForRuntime(
                    CommonProperties.MOXY_JSON_FEATURE_DISABLE,
                    context.getConfiguration().getRuntimeType());
            context.property(disableMoxy, true);

            // add the default Jackson exception mappers and allow jaxb annotations
            context.register(JsonParseExceptionMapper.class);
            context.register(JacksonJaxbJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
            return true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM