Lod*_*ger 5 rest spring spring-data-rest
在我们的应用程序中提供以下工作存储库:
public interface PersonRepository extends PagingAndSortingRepository<Person, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
该存储库通过带有URI“ / api / persons”的spring-data-rest公开,并且可以正常工作。
现在,我们想在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";
}
Run Code Online (Sandbox Code Playgroud)
如果我们将数据发布到“ / api / persons”,则将调用PersonController的方法,但是PersonRepository的任何方法(例如GET)都无法通过rest访问。我们不断收到405错误和以下异常:
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
Run Code Online (Sandbox Code Playgroud)
经过一番摸索之后,我们发现,如果我们将@RequestMapping批注的值属性更改为:
value="/**"
Run Code Online (Sandbox Code Playgroud)
至
value="/save"
Run Code Online (Sandbox Code Playgroud)
阅读此问题和链接的文档后,如果值属性为“ / **”,则它也应该起作用
| 归档时间: |
|
| 查看次数: |
4046 次 |
| 最近记录: |