0

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.

3
  • I don't know that you can configure Jersey to use Jackson using just XML. The Jersey documentation (jersey.java.net/documentation/latest/media.html#json.jackson) indicates that you need to implement a ContextResolver to return the appropriate ObjectMapper for Jersey to use. You might try to add a dependency on jersey-media-json-jackson and make sure you don't have any `MOXy stuff in your classpath.
    – Baldy
    Aug 4, 2014 at 12:43
  • @Baldy ok but how I can register the class which implements the context resolver ?
    – SteveSt
    Aug 7, 2014 at 14:55
  • Did you find an answer?
    – Gijs
    Sep 28, 2015 at 14:35

2 Answers 2

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.