泽西岛 (JAX-RS) 如何使用多个可选参数映射路径



我需要将具有多个可选参数的路径映射到我的端点

路径看起来像localhost/func1/1/2/3localhost/func1/1localhost/func1/1/2,此路径应正确匹配

public Double func1(int p1, int p2, int p3){ ... }

我应该在批注中使用什么?

测试

任务是使用 Jersey 找到使用多个可选参数的方法,而不是学习 REST 设计。

要解决此问题,您需要使参数可选,但也/符号可选

在最终结果中,它看起来类似于这样:

    @Path("func1/{first: ((+|-)?d+)?}{n:/?}{second:((+|-)?d+)?}{p:/?}{third:((+|-)?d+)?}")
    public String func1(@PathParam("first") int first, @PathParam("second") int second, @PathParam("third") int third) {
        ...
    }
你应该

尝试一下QueryParams

@GET
@Path("/func1")
public Double func1(@QueryParam("p1") Integer p1, 
                    @QueryParam("p2") Integer p2, 
                    @QueryParam("p3") Integer p3) {
...
}

这将被请求如下:

localhost/func1?p1=1&p2=2&p3=3

这里所有参数都是可选的。在路径部分中,这是不可能的。服务器无法区分例如:

func1/p1/p3func1/p2/p3

以下是 QueryParam 用法的一些示例:http://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/。

最新更新