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

坚强是说给别人听的谎言 提交于 2019-12-07 05:04:40

问题


I need to map path with multiple optional arguments to my endpoint

path will look like localhost/func1/1/2/3 or localhost/func1/1 or localhost/func1/1/2 and this path should be correctly matched with

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

What should I use in my annotations?

It's test task to play with Jersey to find a way for using multiple optional params, not to learn REST design.


回答1:


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



回答2:


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/.



来源:https://stackoverflow.com/questions/35036982/jersey-jax-rs-how-to-map-path-with-multiple-optional-parameters

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