|
|
|
@ -18,15 +18,21 @@ package org.springframework.boot.autoconfigure.web;
|
|
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import org.junit.After;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
|
import org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.web.JacksonHttpMessageConvertersConfiguration.MappingJackson2HttpMessageConverterConfiguration;
|
|
|
|
|
import org.springframework.boot.test.EnvironmentTestUtils;
|
|
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;
|
|
|
|
|
import org.springframework.hateoas.ResourceSupport;
|
|
|
|
|
import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
|
|
|
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
|
|
|
import org.springframework.http.converter.json.GsonHttpMessageConverter;
|
|
|
|
|
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
|
|
|
@ -36,7 +42,10 @@ import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConve
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
import static org.hamcrest.Matchers.is;
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
import static org.junit.Assert.assertThat;
|
|
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -187,6 +196,39 @@ public class HttpMessageConvertersAutoConfigurationTests {
|
|
|
|
|
assertConverterBeanRegisteredWithHttpMessageConverters(StringHttpMessageConverter.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void typeConstrainedConverterDoesNotPreventAutoConfigurationOfJacksonConverter()
|
|
|
|
|
throws Exception {
|
|
|
|
|
this.context.register(JacksonObjectMapperBuilderConfig.class,
|
|
|
|
|
TypeConstrainedConverterConfiguration.class,
|
|
|
|
|
HttpMessageConvertersAutoConfiguration.class);
|
|
|
|
|
this.context.refresh();
|
|
|
|
|
|
|
|
|
|
BeanDefinition beanDefinition = this.context
|
|
|
|
|
.getBeanDefinition("mappingJackson2HttpMessageConverter");
|
|
|
|
|
assertThat(beanDefinition.getFactoryBeanName(),
|
|
|
|
|
is(equalTo(MappingJackson2HttpMessageConverterConfiguration.class
|
|
|
|
|
.getName())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void typeConstrainedConverterFromSpringDataDoesNotPreventAutoConfigurationOfJacksonConverter()
|
|
|
|
|
throws Exception {
|
|
|
|
|
this.context.register(JacksonObjectMapperBuilderConfig.class,
|
|
|
|
|
RepositoryRestMvcConfiguration.class,
|
|
|
|
|
HttpMessageConvertersAutoConfiguration.class);
|
|
|
|
|
this.context.refresh();
|
|
|
|
|
|
|
|
|
|
Map<String, MappingJackson2HttpMessageConverter> beansOfType = this.context
|
|
|
|
|
.getBeansOfType(MappingJackson2HttpMessageConverter.class);
|
|
|
|
|
System.out.println(beansOfType);
|
|
|
|
|
BeanDefinition beanDefinition = this.context
|
|
|
|
|
.getBeanDefinition("mappingJackson2HttpMessageConverter");
|
|
|
|
|
assertThat(beanDefinition.getFactoryBeanName(),
|
|
|
|
|
is(equalTo(MappingJackson2HttpMessageConverterConfiguration.class
|
|
|
|
|
.getName())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void assertConverterBeanExists(Class<?> type, String beanName) {
|
|
|
|
|
assertEquals(1, this.context.getBeansOfType(type).size());
|
|
|
|
|
List<String> beanNames = Arrays.asList(this.context.getBeanDefinitionNames());
|
|
|
|
@ -254,4 +296,14 @@ public class HttpMessageConvertersAutoConfigurationTests {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
protected static class TypeConstrainedConverterConfiguration {
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
public TypeConstrainedMappingJackson2HttpMessageConverter typeConstrainedConverter() {
|
|
|
|
|
return new TypeConstrainedMappingJackson2HttpMessageConverter(
|
|
|
|
|
ResourceSupport.class);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|