英文:
To use different paths to access same resource in REST API
问题
工作中使用 Spring Boot:MVC,REST API
背景:模型=学生(Student) >> 年龄长整型(Student类的属性之一)
我可以定义两个URL路径来访问特定学生的年龄吗?示例:
- 根据学生的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);
- 根据学生的姓名访问年龄
@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:
- Access by id of student
`
@GetMapping("/{id}/age")
public ResponseEntity<String> getStudentAge(@PathVariable Long id) {
String age = studentService.retrieveAgeById(id);
return new ResponseEntity<String>(age, HttpStatus.OK);
}
`
SQL query (using id ):
@Query("select d.id, d.age from Student d where d.id=:id")
String findAgeById(Long id);
- Access age by name of Student
`
@GetMapping("/{name}/age")
public ResponseEntity<String> getStudentAgeByName(@PathVariable String name) {
String age = studentService.retrieveAgeByName(name);
return new ResponseEntity<String>(age, HttpStatus.OK);
}
`
SQL Query (using name):
@Query("select d.name, d.age from Student d where d.name=:name")
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("/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);
}
But it's better to use request param for non identifer field like name
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论