简体   繁体   English

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

[英]To use different paths to access same resource in REST API

Working with Spring-boot:MVC, REST API使用 Spring-boot:MVC, REST API

Background: Model=Student >> Long age (one of the attributes of Student class)背景:Model=Student >> Long age(Student类的属性之一)

Can I define two URL paths to access the age of a particular student?我可以定义两个 URL 路径来访问特定学生的年龄吗? Example:例子:

  1. Access by id of student通过学生ID访问

` `

@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 ): SQL 查询(使用 id ):

@Query("select d.id, d.age from Student d where d.id=:id")
String findAgeById(Long id);
  1. 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): SQL 查询(使用名称):

@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).出现意外错误(类型=内部服务器错误,状态=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)} 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)}

Because of /{name}/age and /{id}/age are same path.因为/{name}/age/{id}/age是相同的路径。 Here, {name} or {id} is a path variable.这里, {name}{id}是路径变量。

So you are trying to do map two different handler method with the same path.因此,您正在尝试使用相同的路径执行 map 两种不同的处理程序方法。 That's why spring gives you error Ambiguous handler methods mapped这就是为什么 spring 给你错误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但最好将请求参数用于name等非标识符字段

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 REST API-对不同的参数使用相同的URI和相同的方法来调用不同的方法 - REST API- use same URI & same method for different parameter to call different methods 同一个 Jersey REST API 类的多个根路径 - Multiple root paths for same Jersey REST API class REST API 相同 url - 不同的响应类型 - REST API same url - different response type Rest资源文件中的两种方法,它们的@Path相同,但mediaType输出不同 - Two methods in Rest resource file with same @Path but different mediaType output 设计具有不同响应参数但请求相同的REST API - Design REST API with different response parameters but same request 在 Rest API 中找不到处理资源 - Handle Resource not found in Rest API 使用Spring 3过滤器将时间戳嵌入资源路径中? - Use Spring 3 filters to embed timestamp in resource paths? CORS 策略:请求的资源上不存在“访问控制允许来源”header Spring 引导 Rest API & VUE - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Spring Boot Rest API & VUE CORS 策略:请求的资源 Spring Boot Rest API 上不存在“Access-Control-Allow-Origin”标头 - CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource Spring Boot Rest API 如何设计和验证对 jax-rs REST API 中子资源的访问以避免代码重复? - How to design and validate access to a sub-resource in a jax-rs REST API avoiding code duplication?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM