【问题标题】: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,
    ) {
    ..
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-01
      • 2017-03-12
      • 2016-07-21
      • 2013-08-22
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多