Simplify Jackson-related auto-configuration for HATEOAS and Data REST

This commit simplifies the Jackson-related auto-configuration that’s
applied when Spring HATEOAS and Spring Data REST are on the classpath.

Previously, Boot used Jackson2HalModule to apply the HAL-related
ObjectMapper configuration to the context’s primary ObjectMapper. This
was to allow HAL-formatted responses to be sent for requests accepted
application/json (see gh-2147). This had the unwanted side-effect of
polluting the primary ObjectMapper with HAL-specific functionality.
Furthermore, Jackson2HalModule is an internal of Spring HATEOAS that
@olivergierke has asked us to avoid using.

This commit replaces the use of Jackson2HalModule with a new approach.
Now, the message converters of any RequestMappingHandlerAdapter beans
are examined and any TypeConstrainedMappingJackson2HttpMessageConverter
instances are modified to support application/json in addition to their
default support for application/hal+json. This behaviour can be disabled
by setting spring.hateoas.use-hal-as-default-json-media-type to false.
This property is named after Spring Data REST’s configuration option
which has the same effect when using Spring Data REST. The new property
replaces the old spring.hateoas.apply-to-primary-object-mapper property.

Previously, when Spring Data REST was on the classpath,
JacksonAutoConfiguration would be switched off resulting in the context
containing multiple ObjectMappers, none of which was primary.

This commit configures RepositoryRestMvcAutoConfiguration to run after
JacksonAutoConfiguration. This gives the latter a chance to create its
primary ObjectMapper before the former adds its ObjectMapper beans to
the context.

Previously, the actuator’s hypermedia support assumed that the
HttpMessageConverters bean would contain every HttpMessageConverter
being used by Spring MVC. When Spring HATEOAS is on the classpath this
isn’t the case as it post-processes RequestMappingHandlerAdapter beans
and adds a TypeConstrainedMappingJackson2HttpMessageConverter to them.
This wasn’t a problem in the past as the primary ObjectMapper, used by a
vanilla MappingJackson2HttpMessageConverter, was configured with Spring
HATEOAS’sJackson2HalModule. Now that this pollution has been tidied up
the assumption described above no longer holds true. MvcEndpointAdvice,
which adds links to the actuator’s json responses, has been updated
to look at the HttpMessageConverters of every
RequestMappingHandlerAdapter when it’s trying to find a converter to
use to write a response with additional hypermedia links.

Integration tests have been added to spring-boot-actuator to ensure
that the changes described above have not regressed the ability to
configure its json output using spring.jackson.* properties (see
gh-1729).

Closes gh-3891
pull/3986/merge
Andy Wilkinson 9 years ago
parent 028fc04777
commit c55900b433

@ -302,5 +302,10 @@
<artifactId>spring-data-elasticsearch</artifactId> <artifactId>spring-data-elasticsearch</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -41,6 +41,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.hateoas.HypermediaHttpMessageConverterConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.boot.autoconfigure.web.ServerProperties;
@ -53,6 +54,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.hateoas.LinkDiscoverer;
import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.DispatcherServlet;
@ -69,6 +72,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson
* @see EndpointWebMvcAutoConfiguration * @see EndpointWebMvcAutoConfiguration
*/ */
@Configuration @Configuration
@ -193,6 +197,14 @@ public class EndpointWebMvcChildContextConfiguration {
} }
@Configuration
@ConditionalOnClass({ LinkDiscoverer.class })
@Import(HypermediaHttpMessageConverterConfiguration.class)
@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL)
static class HypermediaConfiguration {
}
static class ServerCustomization implements EmbeddedServletContainerCustomizer, static class ServerCustomization implements EmbeddedServletContainerCustomizer,
Ordered { Ordered {

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -61,6 +62,7 @@ import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.TypeUtils; import org.springframework.util.TypeUtils;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.annotation.JsonAnyGetter;
@ -219,7 +221,7 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
public static class MvcEndpointAdvice implements ResponseBodyAdvice<Object> { public static class MvcEndpointAdvice implements ResponseBodyAdvice<Object> {
@Autowired @Autowired
private HttpMessageConverters converters; private List<RequestMappingHandlerAdapter> handlerAdapters;
private Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>(); private Map<MediaType, HttpMessageConverter<?>> converterCache = new ConcurrentHashMap<MediaType, HttpMessageConverter<?>>();
@ -275,13 +277,16 @@ public class EndpointWebMvcHypermediaManagementContextConfiguration {
if (this.converterCache.containsKey(mediaType)) { if (this.converterCache.containsKey(mediaType)) {
return (HttpMessageConverter<Object>) this.converterCache.get(mediaType); return (HttpMessageConverter<Object>) this.converterCache.get(mediaType);
} }
for (HttpMessageConverter<?> converter : this.converters) { for (RequestMappingHandlerAdapter handlerAdapter : this.handlerAdapters) {
for (HttpMessageConverter<?> converter : handlerAdapter
.getMessageConverters()) {
if (selectedConverterType.isAssignableFrom(converter.getClass()) if (selectedConverterType.isAssignableFrom(converter.getClass())
&& converter.canWrite(EndpointResource.class, mediaType)) { && converter.canWrite(EndpointResource.class, mediaType)) {
this.converterCache.put(mediaType, converter); this.converterCache.put(mediaType, converter);
return (HttpMessageConverter<Object>) converter; return (HttpMessageConverter<Object>) converter;
} }
} }
}
return null; return null;
} }

@ -25,10 +25,13 @@ import org.springframework.boot.actuate.health.Health.Builder;
import org.springframework.boot.actuate.health.Status; import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletContext; import org.springframework.mock.web.MockServletContext;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -37,6 +40,7 @@ import static org.junit.Assert.assertEquals;
* Tests for {@link EndpointWebMvcAutoConfiguration} of the {@link HealthMvcEndpoint}. * Tests for {@link EndpointWebMvcAutoConfiguration} of the {@link HealthMvcEndpoint}.
* *
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
*/ */
public class HealthMvcEndpointAutoConfigurationTests { public class HealthMvcEndpointAutoConfigurationTests {
@ -53,12 +57,7 @@ public class HealthMvcEndpointAutoConfigurationTests {
public void testSecureByDefault() throws Exception { public void testSecureByDefault() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(SecurityAutoConfiguration.class, this.context.register(TestConfiguration.class);
ManagementServerPropertiesAutoConfiguration.class,
JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
TestHealthIndicator.class);
this.context.refresh(); this.context.refresh();
Health health = (Health) this.context.getBean(HealthMvcEndpoint.class).invoke( Health health = (Health) this.context.getBean(HealthMvcEndpoint.class).invoke(
null); null);
@ -70,25 +69,33 @@ public class HealthMvcEndpointAutoConfigurationTests {
public void testNotSecured() throws Exception { public void testNotSecured() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(SecurityAutoConfiguration.class, this.context.register(TestConfiguration.class);
JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
TestHealthIndicator.class);
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"management.security.enabled=false"); "management.security.enabled=false");
this.context.refresh(); this.context.refresh();
Health health = (Health) this.context.getBean(HealthMvcEndpoint.class).invoke( Health health = (Health) this.context.getBean(HealthMvcEndpoint.class).invoke(
null); null);
assertEquals(Status.UP, health.getStatus()); assertEquals(Status.UP, health.getStatus());
Health map = (Health) health.getDetails().get( Health map = (Health) health.getDetails().get("test");
"healthMvcEndpointAutoConfigurationTests.Test");
assertEquals("bar", map.getDetails().get("foo")); assertEquals("bar", map.getDetails().get("foo"));
} }
@Component @Configuration
protected static class TestHealthIndicator extends AbstractHealthIndicator { @ImportAutoConfiguration({ SecurityAutoConfiguration.class,
JacksonAutoConfiguration.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class })
static class TestConfiguration {
@Bean
public TestHealthIndicator testHealthIndicator() {
return new TestHealthIndicator();
}
}
static class TestHealthIndicator extends AbstractHealthIndicator {
@Override @Override
protected void doHealthCheck(Builder builder) throws Exception { protected void doHealthCheck(Builder builder) throws Exception {

@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
@ -82,6 +83,7 @@ public class ManagementWebSecurityAutoConfigurationTests {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(SecurityAutoConfiguration.class, this.context.register(SecurityAutoConfiguration.class,
WebMvcAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class,
JacksonAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
@ -110,13 +112,7 @@ public class ManagementWebSecurityAutoConfigurationTests {
public void testWebConfigurationWithExtraRole() throws Exception { public void testWebConfigurationWithExtraRole() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(EndpointAutoConfiguration.class, this.context.register(WebConfiguration.class);
EndpointWebMvcAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
SecurityAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
UserDetails user = getUser(); UserDetails user = getUser();
assertTrue(user.getAuthorities().containsAll( assertTrue(user.getAuthorities().containsAll(
@ -155,14 +151,7 @@ public class ManagementWebSecurityAutoConfigurationTests {
public void testDisableBasicAuthOnApplicationPaths() throws Exception { public void testDisableBasicAuthOnApplicationPaths() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());
this.context.register(HttpMessageConvertersAutoConfiguration.class, this.context.register(WebConfiguration.class);
JacksonAutoConfiguration.class, EndpointAutoConfiguration.class,
EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
SecurityAutoConfiguration.class,
ManagementWebSecurityAutoConfiguration.class,
FallbackWebSecurityAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false"); EnvironmentTestUtils.addEnvironment(this.context, "security.basic.enabled:false");
this.context.refresh(); this.context.refresh();
// Just the management endpoints (one filter) and ignores now plus the backup // Just the management endpoints (one filter) and ignores now plus the backup
@ -248,6 +237,18 @@ public class ManagementWebSecurityAutoConfigurationTests {
Matchers.containsString("realm=\"Spring\"")); Matchers.containsString("realm=\"Spring\""));
} }
@Configuration
@ImportAutoConfiguration({ SecurityAutoConfiguration.class,
WebMvcAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class,
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
FallbackWebSecurityAutoConfiguration.class })
static class WebConfiguration {
}
@EnableGlobalAuthentication @EnableGlobalAuthentication
@Configuration @Configuration
static class AuthenticationConfig { static class AuthenticationConfig {

@ -25,6 +25,7 @@ import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration; import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
@ -32,16 +33,18 @@ import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfi
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration; import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/** /**
* Minimal configuration required to run the Actuator with hypermedia.
*
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
*/ */
@Target(ElementType.TYPE) @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Documented @Documented
@Configuration @Configuration
@Import({ ServerPropertiesAutoConfiguration.class, @ImportAutoConfiguration({ ServerPropertiesAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class, ManagementServerPropertiesAutoConfiguration.class,
EmbeddedServletContainerAutoConfiguration.class, EmbeddedServletContainerAutoConfiguration.class,
DispatcherServletAutoConfiguration.class, JacksonAutoConfiguration.class, DispatcherServletAutoConfiguration.class, JacksonAutoConfiguration.class,

@ -0,0 +1,117 @@
/*
* Copyright 2012-2015 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.actuate.endpoint.mvc;
import org.junit.Test;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.hamcrest.Matchers.startsWith;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
/**
* Integration tests for the Actuator's MVC endpoints.
*
* @author Andy Wilkinson
*/
public class MvcEndpointIntegrationTests {
private AnnotationConfigWebApplicationContext context;
@Test
public void defaultJsonResponseIsNotIndented() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(DefaultConfiguration.class);
MockMvc mockMvc = createMockMvc();
mockMvc.perform(get("/beans")).andExpect(content().string(startsWith("{\"")));
}
@Test
public void jsonResponsesCanBeIndented() throws Exception {
assertIndentedJsonResponse(DefaultConfiguration.class);
}
@Test
public void jsonResponsesCanBeIndentedWhenSpringHateoasIsAutoConfigured()
throws Exception {
assertIndentedJsonResponse(SpringHateoasConfiguration.class);
}
@Test
public void jsonResponsesCanBeIndentedWhenSpringDataRestIsAutoConfigured()
throws Exception {
assertIndentedJsonResponse(SpringDataRestConfiguration.class);
}
private void assertIndentedJsonResponse(Class<?> configuration) throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(configuration);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jackson.serialization.indent-output:true");
MockMvc mockMvc = createMockMvc();
mockMvc.perform(get("/beans")).andExpect(content().string(startsWith("{\n")));
}
private MockMvc createMockMvc() {
this.context.setServletContext(new MockServletContext());
this.context.refresh();
return MockMvcBuilders.webAppContextSetup(this.context).build();
}
@ImportAutoConfiguration({ JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class })
static class DefaultConfiguration {
}
@ImportAutoConfiguration({ HypermediaAutoConfiguration.class,
JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class })
static class SpringHateoasConfiguration {
}
@ImportAutoConfiguration({ HypermediaAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
EndpointAutoConfiguration.class, EndpointWebMvcAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, WebMvcAutoConfiguration.class })
static class SpringDataRestConfiguration {
}
}

@ -21,6 +21,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -40,13 +41,15 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio
* *
* @author Rob Winch * @author Rob Winch
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson
* @since 1.1.0 * @since 1.1.0
*/ */
@Configuration @Configuration
@ConditionalOnWebApplication @ConditionalOnWebApplication
@ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class) @ConditionalOnMissingBean(RepositoryRestMvcConfiguration.class)
@ConditionalOnClass(RepositoryRestMvcConfiguration.class) @ConditionalOnClass(RepositoryRestMvcConfiguration.class)
@AutoConfigureAfter(HttpMessageConvertersAutoConfiguration.class) @AutoConfigureAfter({ HttpMessageConvertersAutoConfiguration.class,
JacksonAutoConfiguration.class })
@Import(SpringBootRepositoryRestMvcConfiguration.class) @Import(SpringBootRepositoryRestMvcConfiguration.class)
public class RepositoryRestMvcAutoConfiguration { public class RepositoryRestMvcAutoConfiguration {

@ -21,23 +21,25 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* {@link ConfigurationProperties properties} for Spring HATEOAS. * {@link ConfigurationProperties properties} for Spring HATEOAS.
* *
* @author Phillip webb * @author Phillip Webb
* @author Andy Wilkinson
* @since 1.2.1 * @since 1.2.1
*/ */
@ConfigurationProperties(prefix = "spring.hateoas") @ConfigurationProperties(prefix = "spring.hateoas")
public class HateoasProperties { public class HateoasProperties {
/** /**
* Specify if HATEOAS support should be applied to the primary ObjectMapper. * Specify if application/hal+json responses should be sent to requests that accept
* application/json.
*/ */
private boolean applyToPrimaryObjectMapper = true; private boolean useHalAsDefaultJsonMediaType = true;
public boolean isApplyToPrimaryObjectMapper() { public boolean getUseHalAsDefaultJsonMediaType() {
return this.applyToPrimaryObjectMapper; return this.useHalAsDefaultJsonMediaType;
} }
public void setApplyToPrimaryObjectMapper(boolean applyToPrimaryObjectMapper) { public void setUseHalAsDefaultJsonMediaType(boolean useHalAsDefaultJsonMediaType) {
this.applyToPrimaryObjectMapper = applyToPrimaryObjectMapper; this.useHalAsDefaultJsonMediaType = useHalAsDefaultJsonMediaType;
} }
} }

@ -16,36 +16,30 @@
package org.springframework.boot.autoconfigure.hateoas; package org.springframework.boot.autoconfigure.hateoas;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor; import org.springframework.context.annotation.Import;
import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.LinkDiscoverers; import org.springframework.hateoas.LinkDiscoverers;
import org.springframework.hateoas.RelProvider;
import org.springframework.hateoas.Resource; import org.springframework.hateoas.Resource;
import org.springframework.hateoas.config.EnableEntityLinks; import org.springframework.hateoas.config.EnableEntityLinks;
import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.hal.CurieProvider;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.plugin.core.Plugin; import org.springframework.plugin.core.Plugin;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -65,8 +59,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@ConditionalOnClass({ Resource.class, RequestMapping.class, Plugin.class }) @ConditionalOnClass({ Resource.class, RequestMapping.class, Plugin.class })
@ConditionalOnWebApplication @ConditionalOnWebApplication
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class, @AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class }) HttpMessageConvertersAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class })
@EnableConfigurationProperties(HateoasProperties.class) @EnableConfigurationProperties(HateoasProperties.class)
@Import(HypermediaHttpMessageConverterConfiguration.class)
public class HypermediaAutoConfiguration { public class HypermediaAutoConfiguration {
@Configuration @Configuration
@ -74,43 +70,6 @@ public class HypermediaAutoConfiguration {
@EnableHypermediaSupport(type = HypermediaType.HAL) @EnableHypermediaSupport(type = HypermediaType.HAL)
protected static class HypermediaConfiguration { protected static class HypermediaConfiguration {
@ConditionalOnClass({ Jackson2ObjectMapperBuilder.class, ObjectMapper.class })
protected static class HalObjectMapperConfiguration {
@Autowired
private HateoasProperties hateoasProperties;
@Autowired(required = false)
private CurieProvider curieProvider;
@Autowired
@Qualifier("_relProvider")
private RelProvider relProvider;
@Autowired(required = false)
private ObjectMapper primaryObjectMapper;
@Autowired
@Qualifier("linkRelationMessageSource")
private MessageSourceAccessor linkRelationMessageSource;
@PostConstruct
public void configurePrimaryObjectMapper() {
if (this.primaryObjectMapper != null
&& this.hateoasProperties.isApplyToPrimaryObjectMapper()) {
registerHalModule(this.primaryObjectMapper);
}
}
private void registerHalModule(ObjectMapper objectMapper) {
objectMapper.registerModule(new Jackson2HalModule());
Jackson2HalModule.HalHandlerInstantiator instantiator = new Jackson2HalModule.HalHandlerInstantiator(
HalObjectMapperConfiguration.this.relProvider,
HalObjectMapperConfiguration.this.curieProvider,
this.linkRelationMessageSource);
objectMapper.setHandlerInstantiator(instantiator);
}
@Bean @Bean
public static HalObjectMapperConfigurer halObjectMapperConfigurer() { public static HalObjectMapperConfigurer halObjectMapperConfigurer() {
return new HalObjectMapperConfigurer(); return new HalObjectMapperConfigurer();
@ -118,8 +77,6 @@ public class HypermediaAutoConfiguration {
} }
}
@Configuration @Configuration
@ConditionalOnMissingBean(EntityLinks.class) @ConditionalOnMissingBean(EntityLinks.class)
@EnableEntityLinks @EnableEntityLinks

@ -0,0 +1,93 @@
/*
* Copyright 2012-2015 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.hateoas;
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.PostConstruct;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
/**
* Configuration for {@link HttpMessageConverter HttpMessageConverters} when hypermedia is
* enabled.
*
* @author Andy Wilkinson
*/
public class HypermediaHttpMessageConverterConfiguration {
@Bean
@ConditionalOnProperty(prefix = "spring.hateoas", name = "use-hal-as-default-json-media-type", matchIfMissing = true)
public static HalMessageConverterSupportedMediaTypesCustomizer halMessageConverterSupportedMediaTypeCustomizer() {
return new HalMessageConverterSupportedMediaTypesCustomizer();
}
/**
* Updates any {@link TypeConstrainedMappingJackson2HttpMessageConverter}s to support
* {@code application/json} in addition to {@code application/hal+json}. Cannot be a
* {@link BeanPostProcessor} as processing must be performed after
* {@code Jackson2ModuleRegisteringBeanPostProcessor} has registered the converter and
* it is unordered.
*/
private static class HalMessageConverterSupportedMediaTypesCustomizer implements
BeanFactoryAware {
private volatile BeanFactory beanFactory;
@PostConstruct
public void customizedSupportedMediaTypes() {
if (this.beanFactory instanceof ListableBeanFactory) {
Map<String, RequestMappingHandlerAdapter> handlerAdapters = ((ListableBeanFactory) this.beanFactory)
.getBeansOfType(RequestMappingHandlerAdapter.class);
for (Entry<String, RequestMappingHandlerAdapter> entry : handlerAdapters
.entrySet()) {
RequestMappingHandlerAdapter handlerAdapter = entry.getValue();
for (HttpMessageConverter<?> converter : handlerAdapter
.getMessageConverters()) {
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
((TypeConstrainedMappingJackson2HttpMessageConverter) converter)
.setSupportedMediaTypes(Arrays.asList(
MediaTypes.HAL_JSON,
MediaType.APPLICATION_JSON));
}
}
}
}
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}
}

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.data.rest;
import java.net.URI; import java.net.URI;
import java.util.Date; import java.util.Date;
import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
@ -25,8 +26,10 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.city.City; import org.springframework.boot.autoconfigure.data.jpa.city.City;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -42,8 +45,11 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/** /**
* Tests for {@link RepositoryRestMvcAutoConfiguration}. * Tests for {@link RepositoryRestMvcAutoConfiguration}.
@ -100,6 +106,15 @@ public class RepositoryRestMvcAutoConfigurationTests {
assertThatDateIsFormattedCorrectly("objectMapper"); assertThatDateIsFormattedCorrectly("objectMapper");
} }
@Test
public void primaryObjectMapperIsAvailable() {
load(TestConfiguration.class);
Map<String, ObjectMapper> objectMappers = this.context
.getBeansOfType(ObjectMapper.class);
assertThat(objectMappers.size(), is(greaterThan(1)));
this.context.getBean(ObjectMapper.class);
}
public void assertThatDateIsFormattedCorrectly(String beanName) public void assertThatDateIsFormattedCorrectly(String beanName)
throws JsonProcessingException { throws JsonProcessingException {
ObjectMapper objectMapper = this.context.getBean(beanName, ObjectMapper.class); ObjectMapper objectMapper = this.context.getBean(beanName, ObjectMapper.class);
@ -111,16 +126,22 @@ public class RepositoryRestMvcAutoConfigurationTests {
private void load(Class<?> config, String... environment) { private void load(Class<?> config, String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.setServletContext(new MockServletContext()); applicationContext.setServletContext(new MockServletContext());
applicationContext.register(config, EmbeddedDataSourceConfiguration.class, applicationContext.register(config, BaseConfiguration.class);
HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(applicationContext, environment); EnvironmentTestUtils.addEnvironment(applicationContext, environment);
applicationContext.refresh(); applicationContext.refresh();
this.context = applicationContext; this.context = applicationContext;
} }
@Configuration
@Import(EmbeddedDataSourceConfiguration.class)
@ImportAutoConfiguration({ HibernateJpaAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class,
RepositoryRestMvcAutoConfiguration.class, JacksonAutoConfiguration.class })
protected static class BaseConfiguration {
}
@Configuration @Configuration
@TestAutoConfigurationPackage(City.class) @TestAutoConfigurationPackage(City.class)
@EnableWebMvc @EnableWebMvc

@ -19,6 +19,9 @@ package org.springframework.boot.autoconfigure.hateoas;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.test.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.EntityLinks;
@ -28,12 +31,21 @@ import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.hal.HalLinkDiscoverer; import org.springframework.hateoas.hal.HalLinkDiscoverer;
import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
@ -41,6 +53,7 @@ import static org.junit.Assert.assertTrue;
* *
* @author Roy Clarkson * @author Roy Clarkson
* @author Oliver Gierke * @author Oliver Gierke
* @author Andy Wilkinson
*/ */
public class HypermediaAutoConfigurationTests { public class HypermediaAutoConfigurationTests {
@ -56,7 +69,8 @@ public class HypermediaAutoConfigurationTests {
@Test @Test
public void linkDiscoverersCreated() throws Exception { public void linkDiscoverersCreated() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.register(HypermediaAutoConfiguration.class); this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
this.context.refresh(); this.context.refresh();
LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class); LinkDiscoverers discoverers = this.context.getBean(LinkDiscoverers.class);
assertNotNull(discoverers); assertNotNull(discoverers);
@ -67,7 +81,8 @@ public class HypermediaAutoConfigurationTests {
@Test @Test
public void entityLinksCreated() throws Exception { public void entityLinksCreated() throws Exception {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.register(HypermediaAutoConfiguration.class); this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
this.context.refresh(); this.context.refresh();
EntityLinks discoverers = this.context.getBean(EntityLinks.class); EntityLinks discoverers = this.context.getBean(EntityLinks.class);
assertNotNull(discoverers); assertNotNull(discoverers);
@ -76,16 +91,23 @@ public class HypermediaAutoConfigurationTests {
@Test @Test
public void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() { public void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.register(SampleConfig.class, HypermediaAutoConfiguration.class); this.context.setServletContext(new MockServletContext());
this.context.register(EnableHypermediaSupportConfig.class, BaseConfig.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jackson.serialization.INDENT_OUTPUT:true");
this.context.refresh(); this.context.refresh();
this.context.getBean(LinkDiscoverers.class); ObjectMapper objectMapper = this.context.getBean("_halObjectMapper",
ObjectMapper.class);
assertThat(
objectMapper.getSerializationConfig().isEnabled(
SerializationFeature.INDENT_OUTPUT), is(false));
} }
@Test @Test
public void jacksonConfigurationIsAppliedToTheHalObjectMapper() { public void jacksonConfigurationIsAppliedToTheHalObjectMapper() {
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.register(JacksonAutoConfiguration.class, this.context.setServletContext(new MockServletContext());
HypermediaAutoConfiguration.class); this.context.register(BaseConfig.class);
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.jackson.serialization.INDENT_OUTPUT:true"); "spring.jackson.serialization.INDENT_OUTPUT:true");
this.context.refresh(); this.context.refresh();
@ -95,9 +117,52 @@ public class HypermediaAutoConfigurationTests {
SerializationFeature.INDENT_OUTPUT)); SerializationFeature.INDENT_OUTPUT));
} }
@Test
public void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
this.context.refresh();
RequestMappingHandlerAdapter handlerAdapter = this.context
.getBean(RequestMappingHandlerAdapter.class);
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
assertThat(
converter.getSupportedMediaTypes(),
containsInAnyOrder(MediaType.APPLICATION_JSON,
MediaTypes.HAL_JSON));
}
}
}
@Test
public void customizationOfSupportedMediaTypesCanBeDisabled() {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.hateoas.use-hal-as-default-json-media-type:false");
this.context.refresh();
RequestMappingHandlerAdapter handlerAdapter = this.context
.getBean(RequestMappingHandlerAdapter.class);
for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) {
if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) {
assertThat(converter.getSupportedMediaTypes(),
contains(MediaTypes.HAL_JSON));
}
}
}
@ImportAutoConfiguration({ HttpMessageConvertersAutoConfiguration.class,
WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class,
HypermediaAutoConfiguration.class })
static class BaseConfig {
}
@Configuration @Configuration
@EnableHypermediaSupport(type = HypermediaType.HAL) @EnableHypermediaSupport(type = HypermediaType.HAL)
static class SampleConfig { static class EnableHypermediaSupportConfig {
} }

Loading…
Cancel
Save