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.pathCreate 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.
}

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.xmlweb.xml
01.
<
web-app
id
=
"WebApp_ID"
version
=
"3.0"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xsi:schemalocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
>
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
>
http://your domain:port/Project Name/url pattern/pathOutput
http://localhost:8080/JAX-RS-Path/rest/cricket – access first method
http://localhost:8080/JAX-RS-Path/rest/cricket/starplayer – access second method
open browser enter the mention url you can see the following output
1. http://localhost:8080/JAX-RS-Path/rest/cricket

2.http://localhost:8080/JAX-RS-Path/rest/cricket/starplayer

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.java01.
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.
}
http://localhost:8080/JAX-RS-Path/rest/shiningstar/Kholi
here Kholi is parameter passed with URL.This parameter accessed by using @PathParam annotation
Output

Regular Expression with Path
PathRegularExpression .java01.
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.
}
Output:
http://localhost:8080/JAX-RS-Path/rest/player/D
accept single character String

http://localhost:8080/JAX-RS-Path/rest/player/24
accept numerical digit

Client
RESTClient.java01.
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 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