We are moved to new domain
Click -> www.ehowtonow.com
Friday, 15 November 2013

JAX-RS Single Param with @PathParm


In this tutorial we are going to see how to pass parameter to RESTful web service via URL using @PathParam
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .

2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder

4. Create package called com.javatutorialscorner.jaxrs.pathparam under RESTful-WebService
5. Create Java class PathParamService under com.javatutorialscorner.jaxrs.pathparam package
PathParamService.java
package com.javatutorialscorner.jaxrs.pathparam;

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

@Path("/tutorial")
public class PathParamService {

@GET
@Path("{index}")
public Response getTutorial(@PathParam("index") String index) {
return Response
.status(200)
.entity("The Tutorial at Index " + index
+ " is RESTful webservice").build();

}
}

In above class @PathParam used to get the value of URL parameter that is defined in @Path expression into Java Class.

Configure Jersey Servlet Dispatcher

you need to configure REST as servlet in web.xml.

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JAX-RS-Path</display-name>
<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.javatutorialscorner.jaxrs.pathparam</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>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RESTful-WebService/rest/tutorial/1


you can call the service by using web browser or from client class.

Web browser call

output Create Client

1.Create package called com.javatutorialscorner.jaxrs.client under RESTful-WebService project

2.Create Client Java Class RESTfulClient under  com.javatutorialscorner.jaxrs.client package

RESTfulClient .java

package com.javatutorialscorner.jaxrs.client;

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 RESTfulClient {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String baseURI = "http://localhost:8080/RESTful-WebService";
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("tutorial/1")
.get(ClientResponse.class).getEntity(String.class));
}

}

Now you can run the client and see the following output in console

output for path param

The Tutorial at Index 1 is RESTful webservice

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RS Single Param with @PathParm Rating: 5 Reviewed By: eHowToNow