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

随声附和 提交于 2019-12-05 10:58:14

To solve this you need to make your params optional, but also / sign optional

In the final result it will looks similar to this:

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

You should give QueryParams a try:

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

which would be requested like:

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

Here all parameters are optional. Within the path part this is not possible. The server could not distinguish between e.g.:

func1/p1/p3 and func1/p2/p3

Here are some examples of QueryParam usage: http://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!