org.glassfish.jersey.examples.jackson1.MyObjectMapperProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-jackson1 Show documentation
Show all versions of json-jackson1 Show documentation
Jersey JSON with Jackson 1.x example.
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.glassfish.jersey.examples.jackson1;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.AnnotationIntrospector.Pair;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.introspect.JacksonAnnotationIntrospector;
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
/**
* @author Jakub Podlesak
*/
@Provider
public class MyObjectMapperProvider implements ContextResolver {
final ObjectMapper defaultObjectMapper;
final ObjectMapper combinedObjectMapper;
public MyObjectMapperProvider() {
defaultObjectMapper = createDefaultMapper();
combinedObjectMapper = createCombinedObjectMapper();
}
@Override
public ObjectMapper getContext(final Class> type) {
if (type == CombinedAnnotationBean.class) {
return combinedObjectMapper;
} else {
return defaultObjectMapper;
}
}
private static ObjectMapper createCombinedObjectMapper() {
final Pair combinedIntrospector = createJaxbJacksonAnnotationIntrospector();
final ObjectMapper result = new ObjectMapper();
result.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
result.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
result.setDeserializationConfig(result.getDeserializationConfig().withAnnotationIntrospector(combinedIntrospector));
result.setSerializationConfig(result.getSerializationConfig().withAnnotationIntrospector(combinedIntrospector));
return result;
}
private static ObjectMapper createDefaultMapper() {
final ObjectMapper result = new ObjectMapper();
result.configure(Feature.INDENT_OUTPUT, true);
return result;
}
private static Pair createJaxbJacksonAnnotationIntrospector() {
final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector();
final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
return new AnnotationIntrospector.Pair(jacksonIntrospector, jaxbIntrospector);
}
}