【问题标题】:REST Resource Naming & @PathVariable annotationREST 资源命名和@PathVariable 注释
【发布时间】:2018-05-28 18:55:39
【问题描述】:
我正在阅读有关 REST 资源命名的教程...
http://api.example.com/device-management/managed-devices/{id}/scripts/{id}:clone
这是命名最佳实践的示例,但我不知道如何使用@PathVariable 注释声明它并区分{id} 和{id}:clone
public ResponseEntity<?> clone (
HttpServletRequest request,
@PathVariable long id, ...) {
..
}
【问题讨论】:
标签:
rest
spring-boot
response
restful-architecture
restful-url
【解决方案1】:
你需要通过变量名或者value和@PathVariable的属性来区分这两个ID,比如:
@GetMapping("http://api.example.com/device-management/managed-devices/{idManagedDevice}/scripts/{idScript}"
public ResponseEntity<?> clone (HttpServletRequest request,
@PathVariable long idManagedDevice,
@PathVariable long idScript,
) {
..
}
或者:
@GetMapping("http://api.example.com/device-management/managed-devices/{id}/scripts/{idScript}"
public ResponseEntity<?> clone (HttpServletRequest request,
@PathVariable(value="id") long idOfManagedDevice,
@PathVariable(value="idScript") long idOfScript,
) {
..
}