Adopt RepositoryRestConfigurer and discourage subclassing
A RepositoryRestMvcConfiguration subclass provided by a user is problematic in a Spring Boot application as it causes RepositoryRestMvcConfiguration's bean declarations to be processed before any auto-configuration runs. One problem that this causes is that it switches off Boot's Jackson auto-configuration due to RepositoryRestMvcConfiguration having already declared multiple ObjectMapper beans. Unlike Boot's auto-configured ObjectMapper, none of these ObjectMappers are marked as @Primary. This then leads to wiring failures due to multiple candidates being available. To address this problem a new RepositoryRestConfigurer abstract has been introduced in Spring Data Gosling. Its use is now strongly preferred over subclassing RepositoryRestMvcConfiguration. Note that our own RepositoryRestMvcConfiguration subclass remains. It is imported as part of auto-configuration (avoiding the ordering problems described above), and provides configuration properties binding for RepositoryRestConfiguration. However, the Jackson ObjectMapper configuration has been moved out into a new RepositoryRestConfigurer implementation. While SpringBootRepositoryRestMvcConfiguration remains, this commit makes it package private to discourage users from subclassing it. While this may break existing applications, it, coupled with the documentation updates, will hopefully guide them toward using RepositoryRestConfigurer. Closes gh-3439pull/3588/head
parent
9cd3b20a78
commit
24c63c9b55
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.data.rest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
/**
|
||||
* A {@code RepositoryRestConfigurer} that applies our configuration to Spring Data REST.
|
||||
* Specifically, if a {@link Jackson2ObjectMapperBuilder} is available, it is used to
|
||||
* configure Spring Data REST's {@link ObjectMapper ObjectMappers}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
class SpringBootRepositoryRestConfigurer extends RepositoryRestConfigurerAdapter {
|
||||
|
||||
@Autowired(required = false)
|
||||
private Jackson2ObjectMapperBuilder objectMapperBuilder;
|
||||
|
||||
@Override
|
||||
public void configureJacksonObjectMapper(ObjectMapper objectMapper) {
|
||||
if (this.objectMapperBuilder != null) {
|
||||
this.objectMapperBuilder.configure(objectMapper);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue