gpt4 book ai didi

java - 具有不同 PathVariable 的相同 rest 端点

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:35:03 25 4
gpt4 key购买 nike

我正在尝试创建两个具有相同 uri 但类型不同的 rest 端点。第一个将按 EAN (Int) 搜索,第二个将按 id (String) 搜索。我可以以某种方式使端点过载吗?我将 Spring Boot 与 Kotlin 结合使用

@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 路径映射的模糊处理程序方法...

最佳答案

我发现如果我想坚持使用我的 API,唯一的方法就是正则表达式。

@GetMapping("/book/{ean:[\\d]+}")

@GetMapping("/book/{id:^[0-9a-fA-F]{24}$}")

有了它MongoDB生成的16进制24个字符就可以和简单的数字区分开来了。如果有人找到更好的方法,请在评论中告诉我。

关于java - 具有不同 PathVariable 的相同 rest 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44499988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com