Example of @PathParam in JAX-RS Web Services

By Arvind Rai, November 19, 2013
On this page we will provide example of @PathParam in JAX-RS web services using Jersey. @PathParam extracts the URI values and matches to @Path. And hence gets the input parameter. @PathParam can be more than one and is set to methods arguments. We will describe it in more detail.

Calculation.java
@Path("/restwb") 
public class Calculation {
	@GET
	@Path("/msg/{p1}/{p2}")
        @Produces("text/plain")
	public String add(@PathParam("p1") Integer param1, @PathParam("p2") Integer param2 ){
		 return String.valueOf(param1+param2);
	}
} 
In the above example,

http://localhost:8080/RestWB/restwb/msg/{p1}/{p2},

p1 matches param1 and p2 matches param2. So for the URI


http://localhost:8080/RestWB/restwb/msg/4/5,

we get the result 9.

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us