Mark Vital
Mark Vital

Reputation: 950

Mapping methods in Jersey framework

Is it possible to map methods to methods calls to URL path both with ID and with parameters? For instance: http://localhost/ws/updateUser/32332?name=John?psw=123

public void updateUser(Sting name, String psw){..}

It seems that current @PathParam annotation supports only parameters in path, like: http://localhost/ws/updateUser/32332/John/123

Upvotes: 4

Views: 734

Answers (2)

Tim Van Laer
Tim Van Laer

Reputation: 2534

You can combine @QueryParam and @PathParam in one method:

@GET
@Path("/user/{userId}")
public ShortLists getShortListsOfUser(@PathParam("userId") String userId,
                                    @QueryParam("pageNumber") Integer pageNumber,
                                    @QueryParam("pageSize") Integer pageSize,
                                    @Context UriInfo uriInfo) {
   /*do something*/
}

This method matches http://localhost/api/user/222?pageNumber=1&pageSize=3


When using a UriBuilder to run this method, mind to use queryParam:

URI uri = getBaseUriBuilder().path("/user/user111").queryParam("pageSize", 2)
              .queryParam("pageNumber", 3).build();

This doesn't work: getBaseUriBuilder().path("/user/user111?pageSize=2&pageNumber=3").build(); (because the question mark is replaced with %3F by the builder)

Upvotes: 0

limc
limc

Reputation: 40176

Try using @QueryParam to capture name and psw parameters:-

public void updateUser(@QueryParam Sting name, @QueryParam String psw) {
   ..
}

Upvotes: 4

Related Questions