Thursday 17, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 4 August 2013

JAX-RS @PATH for URI


In previous tutorials we have seen simple Hello World RESTful Web service using JAX-RS.In this tutorial we will we see @PATH annotation used for URI in RESTful web service.follow the steps given below
Create new Dynamic Web Project and  give project name as JAX-RS-Path



Copy all jar files into WEB-INF/lib folder.Select build path add jar in your build path.

Simple @Path Annotation

Create Class

Create package called com.javatutorialscorner.jaxrs.path
Create following class inside package com.javatutorialscorner.jaxrs.path

RestPathService.java
01.package com.javatutorialscorner.jaxrs.path;
02. 
03.import javax.ws.rs.GET;
04.import javax.ws.rs.Path;
05.import javax.ws.rs.core.Response;
06. 
07.@Path("/cricket")
08.public class RestPathService {
09. 
10. @GET
11. public Response getCaptain() {
12.  return Response.status(200).entity("Captain : Dhoni").build();
13. 
14. }
15. 
16. @GET
17. @Path("/starplayer")
18. public Response getStarPlayer() {
19.  return Response.status(200).entity("Cricket God : Sachin Tendulkar")
20.    .build();
21. 
22. }
23.}
your project directory structure look like this
directory structure
In this class getCaption() resource accessed by @PATH annotation declared for class with base URI and gerStarplayer() resource accessed by base URI + /cricket/starplayer. ‘starplayer’ is annotation for getStrarPlayer() method

Configure Jersey Servlet dispatcher

you need to register REST as servlet dispatcher in web.xml.Configure the following content in web.xml
web.xml
02.  <display-name>JAX-RS-Path</display-name>
03.<servlet>
04.  <servlet-name>jersey-serlvet</servlet-name>
05.  <servlet-class>
06.                     com.sun.jersey.spi.container.servlet.ServletContainer
07.                </servlet-class>
08.  <init-param>
09.       <param-name>com.sun.jersey.config.property.packages</param-name>
10.       <param-value>com.javatutorialscorner.jaxrs.path</param-value>
11.  </init-param>
12.  <load-on-startup>1</load-on-startup>
13. </servlet>
14.  
15. <servlet-mapping>
16.  <servlet-name>jersey-serlvet</servlet-name>
17.  <url-pattern>/rest/*</url-pattern>
18. </servlet-mapping>
19.</web-app>
This is servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer.The init param com.sun.jersey.config.property.packages is used to defines in which package jersey will look for the web service classes.This package point to your resource class package.URL pattern is part of your base URL.Now your web service is read to run.Your Web Service available at following url
http://your domain:port/Project Name/url pattern/path
http://localhost:8080/JAX-RS-Path/rest/cricket – access first method
http://localhost:8080/JAX-RS-Path/rest/cricket/starplayer – access second method
Output
open browser enter the mention url you can see the following output
1. http://localhost:8080/JAX-RS-Path/rest/cricket
class uri

2.http://localhost:8080/JAX-RS-Path/rest/cricket/starplayer
method uri
We used http GET method so we can able to call the resource by browser URL.We will see how to access these resources by using client code before that we will see other options available with @PATH annotation

URI with Path Param

RestPathParamService.java
01.package com.javatutorialscorner.jaxrs.path;
02. 
03.import javax.ws.rs.GET;
04.import javax.ws.rs.Path;
05.import javax.ws.rs.PathParam;
06.import javax.ws.rs.core.Response;
07. 
08.@Path("/shiningstar")
09.public class RestPathParamService {
10. 
11. @GET
12. @Path("{name}")
13. public Response getCricketer(@PathParam("name") String cricketer) {
14.  return Response.status(200).entity("Young Shining Star : " + cricketer)
15.    .build();
16. }
17.}
you can access this resource by using following url
http://localhost:8080/JAX-RS-Path/rest/shiningstar/Kholi
here Kholi is parameter passed with URL.This parameter accessed by using @PathParam annotation
Output
path param

Regular Expression with Path

PathRegularExpression .java
01.package com.javatutorialscorner.jaxrs.path;
02. 
03.import javax.ws.rs.GET;
04.import javax.ws.rs.Path;
05.import javax.ws.rs.PathParam;
06.import javax.ws.rs.core.Response;
07. 
08.@Path("player")
09.public class PathRegularExpression {
10. 
11. @GET
12. @Path("/playerinfo/{crickername : [a-zA-Z]}")
13. public Response getPlayerName(@PathParam("crickername") String cricketer){
14.   
15.  return Response.status(200).entity("Player Name : "+cricketer).build();
16. }
17.  
18. @GET
19. @Path("{age : \\d+}")
20. public Response getPlayerAge(@PathParam("age")String age){
21.  return Response.status(200).entity("Player age : "+age).build();
22. }
23.}
This this service getPlayerName() resource accept only single character string as Path pram in URI. It is accomplished by : [a-zA-Z] this regular expression and second resource getPlayerAge() accept only numerical character \\d used for numerical regular expression
Output:
http://localhost:8080/JAX-RS-Path/rest/player/D
accept single character String
pathregex
http://localhost:8080/JAX-RS-Path/rest/player/24
accept numerical digit
path regex

Client

RESTClient.java
01.package com.javatutorialscorner.jaxrs.path;
02. 
03.import javax.ws.rs.core.MediaType;
04.import javax.ws.rs.core.Response;
05. 
06.import com.sun.jersey.api.client.Client;
07.import com.sun.jersey.api.client.ClientResponse;
08.import com.sun.jersey.api.client.WebResource;
09.import com.sun.jersey.api.client.config.ClientConfig;
10.import com.sun.jersey.api.client.config.DefaultClientConfig;
11. 
12.public class RESTClient {
13. 
14. public static void main(String[] args) {
15.  String baseURI = "http://localhost:8080/JAX-RS-Path";
16.  ClientConfig config = new DefaultClientConfig();
17.  Client client = Client.create(config);
18.  WebResource service = client.resource(baseURI);
19.  System.out.println("output for path param");
20.  System.out.println(service.path("rest").path("shiningstar/Kholi")
21.    .get(ClientResponse.class).getEntity(String.class));
22.  System.out.println("output for smiple path");
23.  System.out.println(service.path("rest").path("cricket")
24.    .get(ClientResponse.class).getEntity(String.class));
25.  System.out.println(service.path("rest").path("cricket/starplayer")
26.    .get(ClientResponse.class).getEntity(String.class));
27.  System.out.println("output for regular expression");
28.  System.out.println(service.path("rest").path("player/playerinfo/D")
29.    .get(ClientResponse.class).getEntity(String.class));
30.  System.out.println(service.path("rest").path("player/24")
31.    .get(ClientResponse.class).getEntity(String.class));
32. 
33. }
34. 
35.}
output for this client :
output for path param
Young Shining Star : Kholi
output for smiple path
Captain : Dhoni
Cricket God : Sachin Tendulkar
output for regular expression
Player Name : D
Player age : 24


Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JAX-RS @QueryParam with @DefaultValueIn this tutorial we are going to see how set  default value to query parameters in RESTful web service using @DefaultValue 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project.2. Cre… Read More
  • Download PDF file using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to download pf file using RESTful web service @Produces("application/pdf") annotation 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Crea… Read More
  • JAX-RS Get HTTP Headers using @HeaderParamIn this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @HeaderParam 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below. 2… Read More
  • JAX-RS XML Service using JAXBIn this tutorial we are going to see how to convert object to XML using JAXB and return back to user using JAX-RS RESTful web service. 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2… Read More
  • JAX-RS Get HTTP Headers using @ContextIn this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @Context 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Proje… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RS @PATH for URI Rating: 5 Reviewed By: eHowToNow