In this example we are going to use @Path annotation. For this
example we are going to have the following files:
- WEB.xml
- PathExample.java
WEB.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>com.tushar.jersey.Project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.tushar.jersey.Project</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
|
PathExample.java
package
com.tushar.jersey.Project;
import
javax.ws.rs.GET;
import
javax.ws.rs.Path;
import
javax.ws.rs.core.Response;
@Path("/path")
public
class PathExample {
@GET
public Response getMsg() {
String output
= "This is an example of Path Annotation";
return
Response.status(200).entity(output).build();
}
@GET @Path("/correct") public Response getUserVIP() { return Response.status(200).entity("this is the correct path").build(); } @GET @Path("{empId : \\d+}") //support digit only public Response getUserById(@PathParam("empId") String empId) { return Response.status(200).entity("Emploee Id is : " + empId).build(); } @GET @Path("/empname/{empname : [a-zA-Z][a-zA-Z_0-9]}") public Response getUserByUserName(@PathParam("empname") String empname) { return Response.status(200) .entity("Employee Name is , empname : " + empname).build(); } @GET @Path("/address/{address : \\d+}") public Response getUserBookByISBN(@PathParam("address") String address) { return Response.status(200) .entity("Address is : " + address).build(); } } |
Run the Server:
Start the server and paste
the following URL in the browser:
- Normal URI Matching : Normal URI matching using @Path Annotation localhost:8080/<project name>/<url-patterndefined in web.xml>/<path defined in class> URI: localhost:8080/com.tushar.jersey.Project/rest/path
Output:

2. Other UI Pattern:
http://localhost:8080/com.tushar.jersey.Project/rest/path/correct
3. Complex UI Pattern:
@Path support
complex URI matching with regular expression, via following expression:
{" variable-name [ ":" regular-expression ] "}
package
com.tushar.jersey.Project;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import
javax.ws.rs.PathParam;
import
javax.ws.rs.core.Response;
@Path("/users")
public class PathComplexURI {
@GET
@Path("{empId : \\d+}") //support digit
only
public Response getUserById(@PathParam("empId") String empId) {
return Response.status(200).entity("Emploee Id
is : " + empId).build();
}
@GET
@Path("/empname/{empname :
[a-zA-Z][a-zA-Z_0-9]}")
public Response getUserByUserName(@PathParam("empname") String empname) {
return Response.status(200)
.entity("Employee
Name is , empname : " + empname).build();
}
@GET
@Path("/address/{address : \\d+}")
public Response getUserBookByISBN(@PathParam("address") String address) {
return Response.status(200)
.entity("Address is
: "
+ address).build();
}
}
|
URI Pattern : “/users/1099”
Output:
URI Pattern : “/users/78966”
Employee Id is : 78966
URI Pattern : “users/empname/s2”
Employee Name is , empname : s2
URI Pattern : “/users/empname/Amit9” , failed, don’t match
“[a-zA-Z][a-zA-Z_0-9]”, first character need “[a-zA-Z]”, second character need
“[a-zA-Z_0-9]”.
Could not find resource for relative :
/users/ empname/Amit9
URI Pattern : “users/address/10”
Address is : 10


No comments:
Post a Comment