鉴于我们的应用程序中有以下工作存储库:
public interface PersonRepository extends PagingAndSortingRepository<Person, Integer> {
}
存储库通过带有 URI“/api/persons”的 spring-data-rest 公开并按预期工作。
我们现在想要在 RestController 的方法中覆盖存储库的 post-method:
@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";
}
如果我们将数据发布到“/api/persons”,则调用 PersonController 的方法,但无法通过 Rest 访问 PersonRepository 的任何方法(例如 GET)。我们不断收到 405 错误和以下异常:
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
经过一番尝试后,我们发现如果我们将 @RequestMapping 注释的 value-property 从
value="/**"
到
value="/save"
看完这篇 question以及链接的文档,如果 value-property 为“/**”,它也应该有效
最佳答案
最后,升级到新版本的 spring/spring-data/spring-data-rest 后,一切正常。
关于 Spring 数据休息 : Override Method in RestController with same request-mapping-path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28455208/