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

Jersey (JAX-RS) how to map path with multiple optional parameters

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

路径看起来像 localhost/func1/1/2/3localhost/func1/1localhost/func1/1/2 并且此路径应与

正确匹配

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

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

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

您应该尝试 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/.

要解决这个问题,您需要将参数设为可选,而且 / 符号可选

最终结果看起来类似于:

    @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) {
        ...
    }