Jaxrs QueryParam作为子项或根项

时间:2014-11-21 20:51:39

标签: jboss jax-rs

我正在处理的设计文档希望我有一个

模式的URL
<root>/v1/installs/XYZ123/actions/next?app=1234ABCD HTTP/1.1

然而,我能找到的唯一示例非常简单,只显示将在/ next结束的URL。 http://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/ 我在想它就像......?我错过了一个关键步骤

@GET
@Produces({ "application/json" })
@Path("v1/installs/{id}/actions<SOMETHINGHERE?>/next HTTP/1.1")
public Response getSetupCommands(@PathParam("id") 
       String id,@QueryParam("next") String next) {

我正在使用jboss和jaxrs

2 个答案:

答案 0 :(得分:1)

为什么我会感觉HTTP/1.1应该成为网址的一部分。您可能错误地阅读/理解了设计文档。如果它说请求应该是

GET /v1/installs/XYZ123/actions/next?app=1234ABCD HTTP/1.1

然后你只需要担心/v1/installs/XYZ123/actions/next?app=1234ABCDHTTP/1.1只是每个请求和响应都会隐式使用的HTTP版本。

您的原始示例很好,例外情况是您应该将@QueryParam("next")替换为@QueryParam("app")next实际上是路径的一部分。

/v1/installs/{id}/actions/next应该是@Path中包含的内容。

此请求URL的完整语义似乎如下所示:

获取next(资源)控制器,我们将使用app查询参数作为参数传递给此控制器。


更新:使用示例

@Path("/v1")
public class QueryResource {
    @GET
    @Path("/installs/{id}/actions/next")
    public Response getResponse(@PathParam("id") String id, 
                                @QueryParam("app") String app) {

        StringBuilder sb = new StringBuilder();
        sb.append("ID: ").append(id).append("<br/>");
        sb.append("app param: ").append(app);
        return Response.ok(sb.toString()).build();
    }
}

浏览器测试

enter image description here

火虫

enter image description here

答案 1 :(得分:0)

v1/installs/{id}/actions/{next : .+}
public Response getSetupCommands(@PathParam("id") String id,@PathParam("next") String next) {

对我的口味太过苛刻了,但在这种情况下我没有任何控制权。

相关问题