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
package com.javatutorialscorner.jaxrs.path;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

@Path("/cricket")
public class RestPathService {

@GET
public Response getCaptain() {
return Response.status(200).entity("Captain : Dhoni").build();

}

@GET
@Path("/starplayer")
public Response getStarPlayer() {
return Response.status(200).entity("Cricket God : Sachin Tendulkar")
.build();

}
}

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

JAX-RS-Path

jersey-serlvet

com.sun.jersey.spi.container.servlet.ServletContainer


com.sun.jersey.config.property.packages
com.javatutorialscorner.jaxrs.path

1



jersey-serlvet
/rest/*


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
package com.javatutorialscorner.jaxrs.path;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/shiningstar")
public class RestPathParamService {

@GET
@Path("{name}")
public Response getCricketer(@PathParam("name") String cricketer) {
return Response.status(200).entity("Young Shining Star : " + cricketer)
.build();
}
}

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
package com.javatutorialscorner.jaxrs.path;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("player")
public class PathRegularExpression {

@GET
@Path("/playerinfo/{crickername : [a-zA-Z]}")
public Response getPlayerName(@PathParam("crickername") String cricketer){

return Response.status(200).entity("Player Name : "+cricketer).build();
}

@GET
@Path("{age : \\d+}")
public Response getPlayerAge(@PathParam("age")String age){
return Response.status(200).entity("Player age : "+age).build();
}
}

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
package com.javatutorialscorner.jaxrs.path;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RESTClient {

public static void main(String[] args) {
String baseURI = "http://localhost:8080/JAX-RS-Path";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(baseURI);
System.out.println("output for path param");
System.out.println(service.path("rest").path("shiningstar/Kholi")
.get(ClientResponse.class).getEntity(String.class));
System.out.println("output for smiple path");
System.out.println(service.path("rest").path("cricket")
.get(ClientResponse.class).getEntity(String.class));
System.out.println(service.path("rest").path("cricket/starplayer")
.get(ClientResponse.class).getEntity(String.class));
System.out.println("output for regular expression");
System.out.println(service.path("rest").path("player/playerinfo/D")
.get(ClientResponse.class).getEntity(String.class));
System.out.println(service.path("rest").path("player/24")
.get(ClientResponse.class).getEntity(String.class));

}

}

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


0 comments

Post a Comment