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-3891pull/3986/merge
parent
028fc04777
commit
c55900b433
@ -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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue