【发布时间】: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