com.sap.cloud.yaas.servicesdk.springboot.jersey.JsonFeatureAutoConfiguration Maven / Gradle / Ivy
/*
* © 2017 SAP SE or an SAP affiliate company.
* All rights reserved.
* Please see http://www.sap.com/corporate-en/legal/copyright/index.epx for additional trademark information and
* notices.
*/
package com.sap.cloud.yaas.servicesdk.springboot.jersey;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
/**
* Registers a {@link Feature} which configures a {@link JacksonJaxbJsonProvider}.
*/
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(ResourceConfig.class)
@ConditionalOnMissingBean(ResourceConfig.class)
@AutoConfigureAfter(JacksonAutoConfiguration.class)
@AutoConfigureBefore(JerseyAutoConfiguration.class)
public class JsonFeatureAutoConfiguration
{
/**
* Registers a {@link Feature} which configures a {@link JacksonJaxbJsonProvider}.
*
* @param jsonMapper the {@link ObjectMapper} that will be used for serialization and deserialization
* of JSON objects
* @return the jackson feature
*/
@Bean
@ConditionalOnBean(ObjectMapper.class)
@ConditionalOnClass(JacksonJaxbJsonProvider.class)
@ConditionalOnProperty(prefix = "yaas.service.jersey", name = "enable-json-feature", matchIfMissing = true)
public Feature jsonFeature(final ObjectMapper jsonMapper)
{
return (FeatureContext context) ->
{
final JacksonJaxbJsonProvider jsonProvider = new JacksonJaxbJsonProvider(jsonMapper,
JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS);
context.register(jsonProvider, MessageBodyReader.class, MessageBodyWriter.class);
return true;
};
}
}