Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get jersey 2 application to use custom jackson XmlMapper

I'm writing web application using Jersey 2.22.1 and Jackson 2.6.3. My pom.xml looks like this:

<!-- JERSEY -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.22.1</version>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.woodstox</groupId>
        <artifactId>woodstox-core-asl</artifactId>
        <version>4.4.1</version>
    </dependency>
    <!-- JACKSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.6.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
        <version>2.6.3</version>
    </dependency>

The goal is to use different custom ObjectMappers for JSON and XML mapping. I've created two provider classes: JSONMapperProvider

    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    public class JSONMapperProvider implements ContextResolver<ObjectMapper> {
    private static ObjectMapper objectMapper;

    public JSONMapperProvider() {
    init();
    }

    public static void init() {
    if (objectMapper == null) {
        objectMapper = new ObjectMapper();
        // SERIALIZATION
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false)
            .setSerializationInclusion(Include.NON_NULL)
            .setSerializationInclusion(Include.NON_EMPTY);
        // DESERIALIZATION
        objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true)
            .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        // set up ISO 8601 date/time stamp format:
        ObjectMapperProvider.DATE_FORMAT_ISO8601.setTimeZone(TimeZone.getTimeZone("GMT"));
        objectMapper.setDateFormat(ObjectMapperProvider.DATE_FORMAT_ISO8601);
        // Custom deserializer for date which helps deserialization of date
        // without time
        SimpleModule dateDeserializerModule = new SimpleModule("DateDeserializerModule", Version.unknownVersion());
        dateDeserializerModule.addDeserializer(Date.class, new CustomJsonDateDeserializer());
        //objectMapper.registerModule(dateDeserializerModule);
    }
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
    System.out.println("JSONMapperProvider.getContext() called with type: "
        + type);
    return objectMapper;
    }

    private static class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {
        String date = jsonparser.getText();
        try {
        return DateUtil.parseDate(date);
        } catch (ParseException e) {
        throw new RuntimeException(e);
        }
    }
    }
}

and XMLMapperProvider

    @Provider
    @Produces({ MediaType.APPLICATION_XML })
    public class XMLMapperProvider implements ContextResolver<ObjectMapper>     {

    private static ObjectMapper objectMapper;

    public XMLMapperProvider() {
    init();
    }

    public static void init() {
    if (objectMapper == null) {
        JacksonXmlModule module = new JacksonXmlModule();
        module.setDefaultUseWrapper(false);
        objectMapper = new XmlMapper(module);
        // SERIALIZATION
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true)
            .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
            .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
            .configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false)
            .setSerializationInclusion(Include.NON_NULL)
            .setSerializationInclusion(Include.NON_EMPTY);
        // DESERIALIZATION
        objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true)
            .configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        // set up ISO 8601 date/time stamp format:
        ObjectMapperProvider.DATE_FORMAT_ISO8601.setTimeZone(TimeZone.getTimeZone("GMT"));
        objectMapper.setDateFormat(ObjectMapperProvider.DATE_FORMAT_ISO8601);
        // Custom deserializer for date which helps deserialization of date
        // without time
        SimpleModule dateDeserializerModule = new SimpleModule("DateDeserializerModule", Version.unknownVersion());
        dateDeserializerModule.addDeserializer(Date.class, new CustomJsonDateDeserializer());
        objectMapper.registerModule(dateDeserializerModule);
    }
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
    System.out.println("XMLMapperProvider.getContext() called with type: "
        + type);
    return objectMapper;
    }

    private static class CustomJsonDateDeserializer extends JsonDeserializer<Date> {
    @Override
    public Date deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {
        String date = jsonparser.getText();
        try {
        return DateUtil.parseDate(date);
        } catch (ParseException e) {
        throw new RuntimeException(e);
        }
    }
    }
}

Both of them are in same package which is registered in Application resource class

@ApplicationPath("resources")
public class CCRestResources extends ResourceConfig {
    public CCRestResources() {
        register(JacksonFeature.class);
        packages("com.cc.rest.jersey");
    }
}

If I make GET request to method that @Produces(MediaType.APPLICATION_JSON) everything is fine and my custom mapper is used. But if I make same request to emthod that @Produces(MediaType.APPLICATION_XML) application use some default mapper not my custom one. Please help if you have any idea. Thanks!

like image 835
Cvetomir Rusenov Avatar asked Mar 14 '23 01:03

Cvetomir Rusenov


1 Answers

So a few things. First you need more than just the core Jackson xml dependency, you need the actual jaxrs provider

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-xml-provider</artifactId>
    <version>${jackson2.version}</version>
</dependency>

Then you should exclude the JAXB provider, which is the default provider used by Jersey. (I had not problems leaving it while testing, but if you're not going to use it, I would just exclude it). It is pulled in by jersey-server, so you should explicitly declare the jersey-server and exclude the jersey-media-jaxb from it

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>${jersey2.version}</version>
    <exclusions>
        <exclusion>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-jaxb</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Then you will need to register the JacksonJaxbXMLProvider (or just the JacksonXMLProvider if you don't need or plan on using JAXB annotations).

public CCRestResources() {
    register(JacksonFeature.class);
    register(JacksonJaxbXMLProvider.class);
    packages("com.cc.rest.jersey");
}

Then finally you need to parameterize the ContextResolver as a XmlMapper type, not ObjectMapper. As seen here, the provider looks for a ContextResolver for XmlMapper, not ObjectMapper.

like image 197
Paul Samsotha Avatar answered Apr 07 '23 14:04

Paul Samsotha