org.glassfish.jersey.examples.jackson.MyObjectMapperProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of json-jackson Show documentation
Show all versions of json-jackson Show documentation
Jersey JSON with Jackson example.
/*
* Copyright (c) 2010, 2022 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.jackson;
import jakarta.ws.rs.ext.ContextResolver;
import jakarta.ws.rs.ext.Provider;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector;
/**
* TODO javadoc.
*
* @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() {
return new ObjectMapper()
.configure(SerializationFeature.WRAP_ROOT_VALUE, true)
.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)
.setAnnotationIntrospector(createJaxbJacksonAnnotationIntrospector());
}
private static ObjectMapper createDefaultMapper() {
final ObjectMapper result = new ObjectMapper();
result.enable(SerializationFeature.INDENT_OUTPUT);
return result;
}
private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {
final AnnotationIntrospector jaxbIntrospector = new JakartaXmlBindAnnotationIntrospector(TypeFactory.defaultInstance());
final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy