【发布时间】:2017-11-13 23:05:47
【问题描述】:
我正在尝试创建两个具有相同 uri 但类型不同的休息端点。 第一个将按 EAN (Int) 搜索,第二个将按 id (String) 搜索。我可以以某种方式重载端点吗?我在 Kotlin 中使用 Spring Boot
@GetMapping("/book/{ean}")
fun getABookByEan(@PathVariable ean: Int) : ResponseEntity<*> {
repository.getByEan(ean)?.let {
return ResponseEntity.status(HttpStatus.OK).body(it)
}
throw ItemNotFoundException()
}
@GetMapping("/book/{id}")
fun getABookById(@PathVariable id: String) : ResponseEntity<*> {
repository.getById(id)?.let {
return ResponseEntity.status(HttpStatus.OK).body(it)
}
throw ItemNotFoundException()
}
在此之后,我遇到了一个异常,即多个方法被映射到同一个端点。
...NestedServletException:请求处理失败;嵌套异常是 java.lang.IllegalStateException: 为 HTTP 路径映射的不明确的处理程序方法...
【问题讨论】:
-
您应该为
/book/byEan/{ean}和/book/byId/{id}创建两个映射
标签: java spring rest spring-boot kotlin