001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.http.codec;
018
019import com.fasterxml.jackson.databind.ObjectMapper;
020
021import org.springframework.boot.autoconfigure.AutoConfigureAfter;
022import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
025import org.springframework.boot.autoconfigure.http.HttpProperties;
026import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
027import org.springframework.boot.context.properties.EnableConfigurationProperties;
028import org.springframework.boot.web.codec.CodecCustomizer;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031import org.springframework.core.annotation.Order;
032import org.springframework.http.codec.CodecConfigurer;
033import org.springframework.http.codec.json.Jackson2JsonDecoder;
034import org.springframework.http.codec.json.Jackson2JsonEncoder;
035import org.springframework.util.MimeType;
036
037/**
038 * {@link EnableAutoConfiguration Auto-configuration} for
039 * {@link org.springframework.core.codec.Encoder Encoders} and
040 * {@link org.springframework.core.codec.Decoder Decoders}.
041 *
042 * @author Brian Clozel
043 * @since 2.0.0
044 */
045@Configuration
046@ConditionalOnClass(CodecConfigurer.class)
047@AutoConfigureAfter(JacksonAutoConfiguration.class)
048public class CodecsAutoConfiguration {
049
050        private static final MimeType[] EMPTY_MIME_TYPES = {};
051
052        @Configuration
053        @ConditionalOnClass(ObjectMapper.class)
054        static class JacksonCodecConfiguration {
055
056                @Bean
057                @Order(0)
058                @ConditionalOnBean(ObjectMapper.class)
059                public CodecCustomizer jacksonCodecCustomizer(ObjectMapper objectMapper) {
060                        return (configurer) -> {
061                                CodecConfigurer.DefaultCodecs defaults = configurer.defaultCodecs();
062                                defaults.jackson2JsonDecoder(
063                                                new Jackson2JsonDecoder(objectMapper, EMPTY_MIME_TYPES));
064                                defaults.jackson2JsonEncoder(
065                                                new Jackson2JsonEncoder(objectMapper, EMPTY_MIME_TYPES));
066                        };
067                }
068
069        }
070
071        @Configuration
072        @EnableConfigurationProperties(HttpProperties.class)
073        static class LoggingCodecConfiguration {
074
075                @Bean
076                @Order(0)
077                public CodecCustomizer loggingCodecCustomizer(HttpProperties properties) {
078                        return (configurer) -> configurer.defaultCodecs()
079                                        .enableLoggingRequestDetails(properties.isLogRequestDetails());
080                }
081
082        }
083
084}