【发布时间】:2020-01-05 03:22:13
【问题描述】:
我的 Spring Boot 应用程序中有这两个 GetMapping 方法:
@GetMapping("/user/{id}")
User one(@PathVariable Long id) {
return repository.findById(id)
.orElseThrow(() -> new UserNotFoundException(id));
}
@GetMapping("/user/{uid}")
User one(@PathVariable String uid) {
return repository.findByDisplayName(uid);
//.orElseThrow(() -> new UserNotFoundException(id));
}
我想通过userID(自动生成)或uniqueUserID(用户创建的String,如果可用)来GetMapping。
但这给了我错误,说:
java.lang.IllegalStateException:为“/user/dis1”映射的不明确的处理程序方法:{com.mua.cse616.Model.User com.mua.cse616.Controller.UserController.one(java.lang.长),com.mua.cse616.Model.User com.mua.cse616.Controller.UserController.one(java.lang.String)}
如何解决?
【问题讨论】:
-
映射一模一样,启动时解析表达式不知道类型。您可以尝试通过在
id部分之后使用额外的正则表达式来限制第一个(数字)。尝试@GetMapping("/user/{id:\\d+}"),但是这假定uid始终包含非数字字符,否则匹配将不起作用。
标签: spring spring-boot jpa h2 path-variables