Spring Data Rest: Override Method in RestController with same request-mapping-path

拜拜、爱过 提交于 2019-12-07 05:50:32

问题


Given the following working repository in our application:

public interface PersonRepository extends PagingAndSortingRepository<Person, Integer> {

}

The Repository is exposed via spring-data-rest with URI "/api/persons" and works as expected.

We now want to override the post-method of the repository in a method of a RestController:

@RestController
@RequestMapping("/persons")
public class PersonController {

@RequestMapping(value = "/**", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> savePerson(@RequestBody Person person) {
      //do something fancy
      return "it works";
}

If we post data to "/api/persons" the method of the PersonController is called but none of the methods of the PersonRepository (e.g. GET) can be accessed via rest. We constantly get an 405 error and the following exception:

org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported

After some playing around we found out that everything works as expected (methods of repository and controller can be called) if we change the value-property of the @RequestMapping annotation from

value="/**"

to

value="/save"

After reading this question and the linked documenation it should also work if the value-property is "/**"


回答1:


Finally, after upgrading to new versions of spring/spring-data/spring-data-rest everything works as expected.



来源:https://stackoverflow.com/questions/28455208/spring-data-rest-override-method-in-restcontroller-with-same-request-mapping-pa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!