/*
 * Copyright 2012-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.autoconfigure.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * {@link EnableAutoConfiguration Auto-configuration} for {@link HttpMessageConverter}s.
 * 
 * @author Dave Syer
 */
@Configuration
@ConditionalOnClass(HttpMessageConverter.class)
public class HttpMessageConvertersAutoConfiguration {

	@Autowired(required = false)
	private final List<HttpMessageConverter<?>> converters = Collections.emptyList();

	@Bean
	@ConditionalOnMissingBean
	public HttpMessageConverters messageConverters() {
		List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>(
				this.converters);
		return new HttpMessageConverters(converters);
	}

	@Configuration
	@ConditionalOnClass(ObjectMapper.class)
	protected static class ObjectMappers {

		@Autowired
		private ListableBeanFactory beanFactory;

		@PostConstruct
		public void init() {
			Collection<ObjectMapper> mappers = BeanFactoryUtils
					.beansOfTypeIncludingAncestors(this.beanFactory, ObjectMapper.class)
					.values();
			Collection<Module> modules = BeanFactoryUtils.beansOfTypeIncludingAncestors(
					this.beanFactory, Module.class).values();
			for (ObjectMapper mapper : mappers) {
				mapper.registerModules(modules);
			}
		}

		@Bean
		@ConditionalOnMissingBean
		@Primary
		public ObjectMapper jacksonObjectMapper() {
			return new ObjectMapper();
		}

		@Bean
		@ConditionalOnMissingBean
		public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(
				ObjectMapper objectMapper) {
			MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
			converter.setObjectMapper(objectMapper);
			return converter;
		}

	}

}

@@ -1228,24 +1228,182 @@
 issingBean;%0A
+import org.springframework.boot.context.properties.ConfigurationProperties;%0Aimport org.springframework.boot.context.properties.EnableConfigurationProperties;%0A
 import org.s
@@ -1924,16 +1924,44 @@
 ve Syer%0A
+ * @author Christian Dupuis%0A
  */%0A@Con
@@ -2495,46 +2495,201 @@
 s)%0A%09
-protected static class ObjectMappers %7B
+@EnableConfigurationProperties(ObjectMappersProperties.class)%0A%09protected static class ObjectMappers %7B%0A%0A%09%09@Autowired%0A%09%09private ObjectMappersProperties properties = new ObjectMappersProperties();
 %0A%0A%09%09
@@ -3567,30 +3567,543 @@
 %0A%09%09%09
-return converter;%0A%09%09%7D%0A
+converter.setPrettyPrint(this.properties.isJsonPrettyPrint());%0A%09%09%09return converter;%0A%09%09%7D%0A%0A%09%7D%0A%0A%09/**%0A%09 * %7B@link ConfigurationProperties%7D to configure %7B@link HttpMessageConverter%7Ds.%0A%09 */%0A%09@ConfigurationProperties(name = %22http.mappers%22, ignoreUnknownFields = false)%0A%09public static class ObjectMappersProperties %7B%0A%0A%09%09private boolean jsonPrettyPrint = false;%0A%0A%09%09public void setJsonPrettyPrint(boolean jsonPrettyPrint) %7B%0A%09%09%09this.jsonPrettyPrint = jsonPrettyPrint;%0A%09%09%7D%0A%0A%09%09public boolean isJsonPrettyPrint() %7B%0A%09%09%09return this.jsonPrettyPrint;%0A%09%09%7D
 %0A%09%7D%0A

Allow pretty printed JSON output (nicer for demos and easier to read)