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

标签 java rest resteasy

我有 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/

相关文章:

web-services - 将 ASP.net 身份生成的不记名 token 传递给 WCF Restful 服务

javascript - 在 Meteor.js 中获取响应 header

java - 使用 ViewPager 和 Fragments 时无法动态更改 textView 的文本

java - 如何在mysql中获取正确的日期和时间格式?

json - Symfony2 FOSRestBundle ParamFetcher JSON 错误

java - 使用 JAX-RS (RESTEasy) 解决 AngularJS JSON 漏洞

java - jax-rs ResponseFilter 中抛出的异常不会导致回滚

java - RestEasy异步请求空指针异常

java - 比较具有涉及 boolean 值的多个属性的列表

java - Play Framework 2.x 无法识别新创建的 scala.html View