【问题标题】:Spring GET same URL but different parameter typesSpring GET 相同的 URL 但不同的参数类型
【发布时间】:2016-07-30 23:28:44
【问题描述】:

所以我正在创建一个用户 API,并尝试在登录前调用 getUserByEmail()。我的问题是我得到了一个为 HTTP 路径错误映射的歧义处理程序方法。

@RequestMapping(value = "/user/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@PathVariable("id") int id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

@RequestMapping(value = "/user/{email}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUserByEmail(@PathVariable("email") String email) {
    System.out.println("Fetching User with email " + email);
    User user = userService.findByEmail(email);
    if (user == null) {
        System.out.println("User with email " + email + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

这是我得到的错误响应:

{"timestamp":1460226184275,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalStateException","message":"为 HTTP 路径映射的模糊处理程序方法'http://localhost:8080/user/fr': {public org.springframework.http.ResponseEntity com.ffa.controllers.UserController.getUser(int), public org.springframework .http.ResponseEntity com.ffa.controllers.UserController.getUserByEmail(java.lang.String)}","path":"/user /fr"}

我知道我的问题与我的 GET 相同但参数类型不同有关。任何帮助将不胜感激!

【问题讨论】:

  • 结合您的映射方法并制定逻辑,根据您的 ID 或电子邮件地址获取使用情况
  • 目前根据您的逻辑 /user/ 不知道它是否是 id 的电子邮件。要么有 /user/ 电子邮件? Email= 和 /user/id?id=id
  • @LearningPhase 我希望他们分开,因为每个人都做不同的事情。如何修改它以便它知道使用哪一个?
  • 使用 /user/id/{id} 和 /user/email/{email}
  • 使用 /user/id/{Id} 和 /user/email/{email} 之类的东西

标签: java spring spring-mvc


【解决方案1】:

您可以使用具有两个不同 URL 的 RequestParams 来代替 PathVariables。

@RequestMapping(value = "/user", params="userID", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<User> getUser(@RequestParam("userID") int id) {
    System.out.println("Fetching User with id " + id);
    User user = userService.findById(id);
    if (user == null) {
        System.out.println("User with id " + id + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

@RequestMapping(value = "/user", params="emailID, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE")
public ResponseEntity<User> getUserByEmail(@RequestParam("emailID") String email) {
    System.out.println("Fetching User with email " + email);
    User user = userService.findByEmail(email);
    if (user == null) {
        System.out.println("User with email " + email + " not found");
        return new ResponseEntity<User>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

当您调用/user?userID=123 时,它将调用第一个,当您调用/user?emailID=something@gamil.com 时,它将调用第二个。这由 @RequestMapping 中传递的 params 属性负责。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2020-04-30
    相关资源
    最近更新 更多