REST Resource Naming & @PathVariable annotation

时间:2018-05-28 18:55:40

标签: rest spring-boot response restful-architecture restful-url

I am reading a tutotral regarding the REST Resource Naming...

http://api.example.com/device-management/managed-devices/{id}/scripts/{id}:clone

This is an example of a best practice in the naming, but I don't know how to declare it using the @PathVariable annotation and distinguish one {id} from {id}:clone

public ResponseEntity<?> clone (
HttpServletRequest request, 
@PathVariable long id, ...) {
..
}

1 个答案:

答案 0 :(得分:0)

您需要通过变量名称或来自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,
) {
..
}