2

I want to pass multiple parameters into my method. How do I do this? I want the url to look like this http://host/one/two/three/four

I have the below code so far

@GET
@Produces({MediaType.APPLICATION_JSON})
@Path("/{one,two,three}") 

public List<Person> getPeople(@PathParam ("one") String one, @PathParam ("two") String two, @PathParam ("three") String three){
   String one = one; 
   String two = two; 
   String three = three;

}

Is this the right syntax for grabbing the params and passing it to my method? I've seen some regular expressions used in the @Path but I don't understand it. I honestly really just want to be able to grab the parameters and put them in a variable if possible.

1
  • 1
    Are you asking for an indeterminate number of parameters or is there a fixed number? You show through 3 in your example, but through 4 in your example url. Commented Aug 3, 2016 at 14:15

1 Answer 1

6

Fixed number of path parameters:

@GET
@Path("/{one}/{two}/{three}")
@Produces(MediaType.APPLICATION_JSON)
public Response foo(@PathParam("one") String one,
                    @PathParam("two") String two,
                    @PathParam("three") String three) {

    ...
}

Variable number of path parameters:

@GET
@Path("/{path: .+}")
@Produces(MediaType.APPLICATION_JSON)
public Response foo(@PathParam("path") String path) {

    String[] paths = path.split("/");

    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Luke Check the documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.