-
Notifications
You must be signed in to change notification settings - Fork 40.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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-3439
- Loading branch information
1 parent
9cd3b20
commit 24c63c9
Showing
5 changed files
with
58 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../org/springframework/boot/autoconfigure/data/rest/SpringBootRepositoryRestConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters