Integrate Jersey RESTful with portlet project

General Blogs May 3, 2013 By Pradip A Bhatt

Hello All Friends...

 

Recently I have succssfully integrate Jersey Framework with my existing portlet project.

It is very easy and very useful..

 

For this, you have to follow following steps..

 

1) Add following dependency if you are using maven, otherwise add this jars to your classpat.

<!-- Jersey -->

 

<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-server</artifactId>
        <version>1.8</version>
</dependency>
 
<dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.8</version>
</dependency>
 
2) Edit in web.xml
 
<servlet>
          <servlet-name>jersey-serlvet</servlet-name>
          <servlet-class>com.sun.jersey.config.property.packages</servlet-class>
          <init-param>
                    <param-name>com.sun.jersey.config.property.packages</param-name>
                   <!-- Here, you have to mention package name where toy are implementd service(web service)-->
                    <param-value>com.hrms.admin.jersey.service</param-value>
          </init-param>
          <init-param>
          <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
                    <param-value>true</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>
 
 
3) Look out Web service class. 
Example..
 
@Path("/mymessage")
public class MyWebServiceClass
{
       @GET
       @Path("hello")
       @Produces(MediaType.APPLICATION_JSON)
       public Message myMethod()
      {
              MyMessage msg = new MyMessage();
              msg.setName("Good Day!!!");
              msg.setTitle("Welcome");
 
             return msg;
      }
}
 
-> here, we can define @Path at two level. 1) class level 2) path level.
-> so Here, i provide both ultimately url become... <...>/mymessage/hello
-> @Produces(MediaType.APPLICATION_JSON) defines that this method returns data in JSON format.
 
This is web service class where we implelent service. 
 
 
4) Now we have to create client which call this service.
 
<dependency>
         <groupId>com.sun.jersey</groupId>
         <artifactId>jersey-client</artifactId>
         <version>1.8</version>
</dependency>
 
I am using GSON. So..
 
<!-- GSON -->
<dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>1.7.1</version>
</dependency>
 
5) Client program
 
public class MyClient {
 
       Logger log = Logger.getLogger(MyClient.class);
 
       public String createClient(String url) {
       try {
                     Client client = Client.create();
                     String baseURL = "http://localhost:8080/admin-0.0.1/rest";
                     WebResource webResource = client.resource(baseURL + url);
                     ClientResponse clientResponse = webResource.accept("application/json").get(ClientResponse.class);
 
                     if (clientResponse.getStatus() != 200) {
                                        throw new RuntimeException("Failed...... HTTP Error Code :"
                                                         + clientResponse.getStatus());
                                           }
                             }
 
                    String output = clientResponse.getEntity(String.class);
 
                     System.out.println("Output from Server :");
 
                     System.out.println(output);
 
                     return output;
                    } catch (Exception e) {
                          e.getMessage();
                        return "ERROR";
               }
        }
}
 
--> Here... http://localhost:8080/admin-0.0.1/rest define as a Base URL. and at the time of calling any service we have to pass remaining parameter in it.
 
like..
 
        MyClient client = new MyClient();
       String result = client.createClient("/mymessage/hello");
 
 
So... It will return MyMessage object in JSON format.
 
 
Thanksssssssssssssssssssss
Showing 1 result.