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; 018 019import java.util.List; 020import java.util.stream.Collectors; 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.autoconfigure.jsonb.JsonbAutoConfiguration; 030import org.springframework.boot.context.properties.EnableConfigurationProperties; 031import org.springframework.context.annotation.Bean; 032import org.springframework.context.annotation.Configuration; 033import org.springframework.context.annotation.Import; 034import org.springframework.http.converter.HttpMessageConverter; 035import org.springframework.http.converter.StringHttpMessageConverter; 036 037/** 038 * {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s. 039 * 040 * @author Dave Syer 041 * @author Christian Dupuis 042 * @author Piotr Maj 043 * @author Oliver Gierke 044 * @author David Liu 045 * @author Andy Wilkinson 046 * @author Sebastien Deleuze 047 * @author Stephane Nicoll 048 * @author EddĂș MelĂ©ndez 049 */ 050@Configuration 051@ConditionalOnClass(HttpMessageConverter.class) 052@AutoConfigureAfter({ GsonAutoConfiguration.class, JacksonAutoConfiguration.class, 053 JsonbAutoConfiguration.class }) 054@Import({ JacksonHttpMessageConvertersConfiguration.class, 055 GsonHttpMessageConvertersConfiguration.class, 056 JsonbHttpMessageConvertersConfiguration.class }) 057public class HttpMessageConvertersAutoConfiguration { 058 059 static final String PREFERRED_MAPPER_PROPERTY = "spring.http.converters.preferred-json-mapper"; 060 061 private final List<HttpMessageConverter<?>> converters; 062 063 public HttpMessageConvertersAutoConfiguration( 064 ObjectProvider<HttpMessageConverter<?>> convertersProvider) { 065 this.converters = convertersProvider.orderedStream().collect(Collectors.toList()); 066 } 067 068 @Bean 069 @ConditionalOnMissingBean 070 public HttpMessageConverters messageConverters() { 071 return new HttpMessageConverters(this.converters); 072 } 073 074 @Configuration 075 @ConditionalOnClass(StringHttpMessageConverter.class) 076 @EnableConfigurationProperties(HttpProperties.class) 077 protected static class StringHttpMessageConverterConfiguration { 078 079 private final HttpProperties.Encoding properties; 080 081 protected StringHttpMessageConverterConfiguration(HttpProperties httpProperties) { 082 this.properties = httpProperties.getEncoding(); 083 } 084 085 @Bean 086 @ConditionalOnMissingBean 087 public StringHttpMessageConverter stringHttpMessageConverter() { 088 StringHttpMessageConverter converter = new StringHttpMessageConverter( 089 this.properties.getCharset()); 090 converter.setWriteAcceptCharset(false); 091 return converter; 092 } 093 094 } 095 096}