After Spring Boot Upgrade to version 2.5.6 I am not able to compile the project because of the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restHandlerMapping' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.webmvc.config.DelegatingHandlerMapping]: Factory method 'restHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: Spring Data REST controller MyController must not use @RequestMapping on class level as this would cause double registration with Spring MVC!
This is my controller:
@BasePathAwareController
@RequestMapping("/api/my")
public class MyController {
@GetMapping("/x")
public ResponseEntity<...> x
@GetMapping("/y")
public ResponseEntity<...> y
@GetMapping("/z")
public ResponseEntity<...> z
}
I was able to solve the problem by removing @RequestMapping
annotation and moving "/api/my" to each method, but is there any way to change it on the class level?
EDIT: To be precise, there is already a config class
@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"
...
}
so in fact my controller does not contain base uri ("/api"), previously I just wanted to simplify the example as much as possible.
@BasePathAwareController
@RequestMapping("/my")
public class MyController {
@GetMapping("/x")
public ResponseEntity<...> x
@GetMapping("/y")
public ResponseEntity<...> y
@GetMapping("/z")
public ResponseEntity<...> z
}
Does it mean that the only way is to define constant paths for each Controller? (e.g. Constants.MY_CONTROLLER_BASE_URI)
CodePudding user response:
Try using the following configuration, which is intended to be used by Spring Data REST to expose repository resources:
spring.data.rest.basePath=/api/my
And defining the controller as:
@BasePathAwareController
public class MyController {
@GetMapping("/x")
public ResponseEntity<...> x
@GetMapping("/y")
public ResponseEntity<...> y
@GetMapping("/z")
public ResponseEntity<...> z
}
CodePudding user response:
Add
server.servlet.context-path=/api/my
to your application.properties equivalent thing if using yaml