Jersey (JAX-RS) how to display a path with several optional parameters

I need to map the path to a few optional arguments for my endpoint

will look like localhost/func1/1/2/3 or localhost/func1/1 or localhost/func1/1/2 , and this path should be properly mapped to

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

What should I use in my annotations?

This is a test task for playing with Jersey to find a way to use a few optional parameters, and not to learn about REST design.

+5
source share
2 answers

To solve this problem you need to make optional parameters, but also / add optional

The final result will look something like 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) { ... } 
+7
source

You should try QueryParams :

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

which will be requested as follows:

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

Here, all parameters are optional. This is not possible within part of the journey. The server could not distinguish, for example:

func1/p1/p3 and func1/p2/p3

The following are examples of using QueryParam : http://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/ .

+5
source

Source: https://habr.com/ru/post/1241605/


All Articles