使用不同路径访问同一资源的REST API

huangapple go评论72阅读模式
英文:

To use different paths to access same resource in REST API

问题

工作中使用 Spring Boot:MVC,REST API

背景:模型=学生(Student) >> 年龄长整型(Student类的属性之一)

我可以定义两个URL路径来访问特定学生的年龄吗?示例:

  1. 根据学生的ID访问
@GetMapping("/{id}/age")
public ResponseEntity<String> getStudentAge(@PathVariable Long id) {
    String age = studentService.retrieveAgeById(id);
    return new ResponseEntity<String>(age, HttpStatus.OK);
}

SQL查询(使用ID):

@Query("select d.id, d.age from Student d where d.id=:id")
String findAgeById(Long id);
  1. 根据学生的姓名访问年龄
@GetMapping("/{name}/age")
public ResponseEntity<String> getStudentAgeByName(@PathVariable String name) {
    String age = studentService.retrieveAgeByName(name);
    return new ResponseEntity<String>(age, HttpStatus.OK);
}

SQL查询(使用姓名):

@Query("select d.name, d.age from Student d where d.name=:name")
String findAgeByName(String name);

该方法产生以下错误:

出现了意外错误(类型=内部服务器错误,状态=500)。多个处理程序方法映射到'/2/age':{public org.springframework.http.ResponseEntity com.example.restapi.controller.StudentController.getStudentAgeByName(java.lang.String),public org.springframework.http.ResponseEntity com.example.restapi.controller.StudentController.getStudentAge(java.lang.Long)}

英文:

Working with Spring-boot:MVC , REST API

Background: Model=Student >> Long age (one of the attributes of Student class)

Can I define two URL paths to access the age of a particular student? Example:

  1. Access by id of student

`

@GetMapping(&quot;/{id}/age&quot;)
public ResponseEntity&lt;String&gt; getStudentAge(@PathVariable Long id) {
    String age = studentService.retrieveAgeById(id);
    return new ResponseEntity&lt;String&gt;(age, HttpStatus.OK);
}

`

SQL query (using id ):

@Query(&quot;select d.id, d.age from Student d where d.id=:id&quot;)
String findAgeById(Long id);
  1. Access age by name of Student

`

   @GetMapping(&quot;/{name}/age&quot;)
        public ResponseEntity&lt;String&gt;  getStudentAgeByName(@PathVariable String name) {
            String age = studentService.retrieveAgeByName(name);
            return new ResponseEntity&lt;String&gt;(age, HttpStatus.OK);
        }

`

SQL Query (using name):

@Query(&quot;select d.name, d.age from Student d where d.name=:name&quot;)
    String findAgeByName(String name);

This method is producing this error:

> There was an unexpected error (type=Internal Server Error,
> status=500). Ambiguous handler methods mapped for '/2/age': {public
> org.springframework.http.ResponseEntity
> com.example.restapi.controller.StudentController.getStudentAgeByName(java.lang.String),
> public org.springframework.http.ResponseEntity
> com.example.restapi.controller.StudentController.getStudentAge(java.lang.Long)}

答案1

得分: 1

因为 /{name}/age/{id}/age 是相同的路径。
在这里,{name}{id} 是路径变量。

所以您试图将两个不同的处理程序方法映射到相同的路径。这就是为什么Spring给出了Ambiguous handler methods mapped错误的原因。

您可以尝试以下方法来解决这个问题:

@GetMapping("/age/{id}")
public ResponseEntity<String> getStudentAge(@PathVariable Long id) {
    String age = studentService.retrieveAgeById(id);
    return new ResponseEntity<String>(age, HttpStatus.OK);
}

@GetMapping("/age/name/{name}")
public ResponseEntity<String> getStudentAgeByName(@PathVariable String name) {
    String age = studentService.retrieveAgeByName(name);
    return new ResponseEntity<String>(age, HttpStatus.OK);
}

但最好对于非标识字段像 name 使用请求参数。

英文:

Because of /{name}/age and /{id}/age are same path.
Here, {name} or {id} is a path variable.

So you are trying to do map two different handler method with the same path. That's why spring gives you error Ambiguous handler methods mapped

You can try this way to resolve this

@GetMapping(&quot;/age/{id}&quot;)
public ResponseEntity&lt;String&gt; getStudentAge(@PathVariable Long id) {
    String age = studentService.retrieveAgeById(id);
    return new ResponseEntity&lt;String&gt;(age, HttpStatus.OK);
}

@GetMapping(&quot;/age/name/{name}&quot;)
public ResponseEntity&lt;String&gt;  getStudentAgeByName(@PathVariable String name) {
     String age = studentService.retrieveAgeByName(name);
     return new ResponseEntity&lt;String&gt;(age, HttpStatus.OK);
}

But it's better to use request param for non identifer field like name

huangapple
  • 本文由 发表于 2020年4月9日 21:54:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/61122776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定