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!