Example of @Path in JAX-RS Web Services

By Arvind Rai, November 18, 2013
On this page we will provide example of @Path in JAX-RS web services using Jersey. @Path in JAX-RS Web Services is the relative path for accessing the web service class. This plays the role for creating URI. It depends on us how long relative path is required.

Calculation.java
package com.concretepage.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
@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);
	}
} 
Suppose we have the application URI as
http://localhost:8080/RestWB
@Path("/restwb") will append the "/restwb" in URI as
http://localhost:8080/RestWB/restwb,
By this URL, our RESTFul class is accessible but still it does not access the method. Now @Path("/msg/{p1}/{p2}") will add "msg" in URI as
http://localhost:8080/RestWB/restwb/msg
and two parameter p1 and p2 is user input. For the URL like
http://localhost:8080/RestWB/restwb/msg/4/5,
4 and 5 is user input and will be used for processing. We have used the add method so it will add and will result in 9.

Download Source Code for Complete Example

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us