Spring Boot 升級到 2.5.6 版本后,我無法編譯專案,因為以下錯誤:
引起:org.springframework.beans.factory.BeanCreationException:在類路徑資源[org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]中定義名稱為“restHandlerMapping”的bean創建錯誤:通過工廠方法的Bean實體化失敗; 嵌套例外是 org.springframework.beans.BeanInstantiationException:無法實體化 [org.springframework.data.rest.webmvc.config.DelegatingHandlerMapping]:工廠方法“restHandlerMapping”拋出例外;嵌套例外是 java.lang.IllegalStateException:Spring Data REST 控制器 MyController 不得在類級別使用 @RequestMapping,因為這會導致與 Spring MVC 的雙重注冊!
這是我的控制器:
@BasePathAwareController
@RequestMapping("/api/my")
public class MyController {
@GetMapping("/x")
public ResponseEntity<...> x
@GetMapping("/y")
public ResponseEntity<...> y
@GetMapping("/z")
public ResponseEntity<...> z
}
我能夠通過洗掉@RequestMapping注釋并將“/api/my”移動到每個方法來解決問題,但是有什么方法可以在類級別更改它?
編輯: 準確地說,已經有一個配置類
@Configuration
public class RestModuleConfiguration implements RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) {
config.setReturnBodyOnCreate(true);
config.setReturnBodyOnUpdate(true);
config.setBasePath(Constants.API_BASE_URI); // "/api"
...
}
所以實際上我的控制器不包含基本 uri(“/api”),以前我只是想盡可能地簡化示例。
@BasePathAwareController
@RequestMapping("/my")
public class MyController {
@GetMapping("/x")
public ResponseEntity<...> x
@GetMapping("/y")
public ResponseEntity<...> y
@GetMapping("/z")
public ResponseEntity<...> z
}
這是否意味著唯一的方法是為每個控制器定義常量路徑?(例如 Constants.MY_CONTROLLER_BASE_URI)
uj5u.com熱心網友回復:
嘗試使用以下配置,旨在供 Spring Data REST 用于公開存盤庫資源:
spring.data.rest.basePath=/api/my
并將控制器定義為:
@BasePathAwareController
public class MyController {
@GetMapping("/x")
public ResponseEntity<...> x
@GetMapping("/y")
public ResponseEntity<...> y
@GetMapping("/z")
public ResponseEntity<...> z
}
uj5u.com熱心網友回復:
添加
server.servlet.context-path=/api/my
如果使用 yaml,則與您的 application.properties 等效
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/366762.html
