gpt4 book ai didi

java - 两个 URL 之间的容易混淆

转载 作者:行者123 更新时间:2023-11-30 11:21:35 24 4
gpt4 key购买 nike

我有 2 个不同的 URL:

GET /stuff/{id} (where id is an Integer)
GET /stuff/foo?bar={someValue} (foo is not an Integer, it is a hard coded String)

当调用/stuff/foo&bar=someValue时,出现以下错误:

Failed executing GET /stuff/foo&bar=someValue
...
Caused by: java.lang.NumberFormatException: For input string: "foo&bar=someValue"

代码是:

@GET
@Path("/stuff/{id}")
public Response getById(@PathParam("id") int id) {
// code
}

@GET
@Path("/stuff/foo")
public Response foo(@QueryParam("bar") String bar) {
// code
}

我正在使用 RESTEasy如果可能的话,我想保留我的 URL。显然,RESTEasy 只是在应该使用 foo 方法的地方尝试了 getById 方法。

我怎样才能在 RESTEasy 中完成这项工作? (如果没有关于 RESTEasy 限制的详细解释,“更改您的 URL”不是答案)。我尝试(确定)将 foo 代码放在 getById 之前,但我有同样的错误(当然)。

声明的 URL 之间是否有优先级概念?

请注意:我已经在另一个框架 (python-flask) 中实现了这种 URL,它工作得很好:你只需要小心声明/stuff/foo before/stuff/{id}(在更通用的案例之前的特定案例)。


编辑:我刚刚犯了一个愚蠢的错误!我正在调用 /stuff/foo&bar=someValue,而我应该调用 /stuff/foo?bar=someValue。感谢@Scobal 指出!

最佳答案

您正在调用 GET/stuff/foo&bar=someValue

您应该调用 GET/stuff/foo?bar=someValue

RESTEasy 正在尝试将 foo&bar=someValue 解析为 {id} 字段。

我无法给你关于 RESTEasy URL 优先级的答案,但你可以这样做:

@GET
@Path("/stuff/{id}")
public Response getById(@PathParam("id") String id, @QueryParam("bar") String bar) {
try {
int intId = Integer.parseInt(id);
// do int id things
} catch(NumberFormatException e) {
// do foo + bar things
}
}

关于java - 两个 URL 之间的容易混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170584/

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