001/* 002 * Copyright 2012-2016 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.web; 018 019import java.util.Collections; 020import java.util.List; 021 022import org.springframework.beans.factory.ObjectProvider; 023import org.springframework.boot.autoconfigure.AutoConfigureAfter; 024import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 027import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration; 028import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; 029import org.springframework.boot.context.properties.EnableConfigurationProperties; 030import org.springframework.context.annotation.Bean; 031import org.springframework.context.annotation.Configuration; 032import org.springframework.context.annotation.Import; 033import org.springframework.http.converter.HttpMessageConverter; 034import org.springframework.http.converter.StringHttpMessageConverter; 035 036/** 037 * {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s. 038 * 039 * @author Dave Syer 040 * @author Christian Dupuis 041 * @author Piotr Maj 042 * @author Oliver Gierke 043 * @author David Liu 044 * @author Andy Wilkinson 045 * @author Sebastien Deleuze 046 * @author Stephane Nicoll 047 */ 048@Configuration 049@ConditionalOnClass(HttpMessageConverter.class) 050@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class }) 051@Import({ JacksonHttpMessageConvertersConfiguration.class, 052 GsonHttpMessageConvertersConfiguration.class }) 053public class HttpMessageConvertersAutoConfiguration { 054 055 static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper"; 056 057 private final List<HttpMessageConverter<?>> converters; 058 059 public HttpMessageConvertersAutoConfiguration( 060 ObjectProvider<List<HttpMessageConverter<?>>> convertersProvider) { 061 this.converters = convertersProvider.getIfAvailable(); 062 } 063 064 @Bean 065 @ConditionalOnMissingBean 066 public HttpMessageConverters messageConverters() { 067 return new HttpMessageConverters(this.converters == null 068 ? Collections.<HttpMessageConverter<?>>emptyList() : this.converters); 069 } 070 071 @Configuration 072 @ConditionalOnClass(StringHttpMessageConverter.class) 073 @EnableConfigurationProperties(HttpEncodingProperties.class) 074 protected static class StringHttpMessageConverterConfiguration { 075 076 private final HttpEncodingProperties encodingProperties; 077 078 protected StringHttpMessageConverterConfiguration( 079 HttpEncodingProperties encodingProperties) { 080 this.encodingProperties = encodingProperties; 081 } 082 083 @Bean 084 @ConditionalOnMissingBean 085 public StringHttpMessageConverter stringHttpMessageConverter() { 086 StringHttpMessageConverter converter = new StringHttpMessageConverter( 087 this.encodingProperties.getCharset()); 088 converter.setWriteAcceptCharset(false); 089 return converter; 090 } 091 092 } 093 094}