« Back

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
Threaded Replies Author Date
Awesome dude... you rock :) Arun kumar September 15, 2013 6:22 AM
Really??? Is this become helpful to you??? Pradip A Bhatt September 15, 2013 9:34 PM
yes. This gave me some idea. Thanks. Can you... Arun kumar September 16, 2013 3:32 AM
yes. This gave me some idea. Thanks. Can you... Arun kumar September 16, 2013 3:32 AM
ca you please answer my question posted in... Arun kumar September 16, 2013 10:00 AM
ca you please answer my question posted in... Arun kumar September 16, 2013 10:00 AM
Hello, I have tried to implement your solution... Dmitriy Martynyuk January 31, 2014 5:06 AM
First of tell when this error occured?? ... Pradip A Bhatt January 31, 2014 5:38 AM
This error occured during deployment process. ... Dmitriy Martynyuk January 31, 2014 9:43 AM
Hi Pradip, Please tell you have integrated... Sneha Jajoo July 16, 2014 3:52 AM
Spring 3 already provides rest support, why we... sravan kumar February 21, 2014 3:28 AM
Have you got this approach to work with Jersey... Wessel Oosthuizen April 9, 2014 12:34 PM
thanks for this post. it's helpfull.. But the... Aymen Debbabi March 2, 2015 8:34 AM

Awesome dude... you rock emoticon
Posted on 9/15/13 6:22 AM.
Really???

Is this become helpful to you???
Posted on 9/15/13 9:34 PM in reply to Arun kumar.
yes. This gave me some idea. Thanks. Can you please let me know if I can do it without the jersey project. I have a portlet created newly of my own. Can you please tell me what I should do to make it a restful Web service?

1. Its an empty portlet. Just did "mvn liferay:build-service"
2. Is it fine, if I just follow the above steps? I cannot figure out how to where to write this MyWebServiceClass and what to write instead of jersey-servlet.

Please suggest.
Posted on 9/16/13 3:32 AM in reply to Pradip A Bhatt.
yes. This gave me some idea. Thanks. Can you please let me know if I can do it without the jersey project. I have a portlet created newly of my own. Can you please tell me what I should do to make it a restful Web service?

1. Its an empty portlet. Just did "mvn liferay:build-service"
2. Is it fine, if I just follow the above steps? I cannot figure out how to where to write this MyWebServiceClass and what to write instead of jersey-servlet.

Please suggest.
Posted on 9/16/13 3:32 AM in reply to Pradip A Bhatt.
ca you please answer my question posted in http://stackoverflow.com/questions/18832785/is-a-servlet-inside-a-portlet-requir­ed-to-make-the-portlet-as-a-web-service

related to your above post.
Posted on 9/16/13 10:00 AM in reply to Arun kumar.
ca you please answer my question posted in http://stackoverflow.com/questions/18832785/is-a-servlet-inside-a-portlet-requir­ed-to-make-the-portlet-as-a-web-service

related to your above post.
Posted on 9/16/13 10:00 AM in reply to Arun kumar.
Hello, I have tried to implement your solution but have faced with the follling exception:

SEVERE: Servlet /portlets-1.0-SNAPSHOT threw load() exception
java.lang.ClassNotFoundException: com.sun.jersey.config.property.packages
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:17­14)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:15­59)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager­.java:527)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(Default­InstanceManager.java:509)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManag­er.java:137)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
­at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123­)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407­)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
­at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1114)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1672)­
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
­at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
­at java.lang.Thread.run(Thread.java:744)


Can you please advice what can cause such issue?
Posted on 1/31/14 5:06 AM.
First of tell when this error occured??

another thing First you have to understand this one.

http://www.mkyong.com/webservices/jax-rs/jersey-spring-integration-example/
Posted on 1/31/14 5:38 AM in reply to Dmitriy Martynyuk.
This error occured during deployment process.

Looks like I have found an issue. Seems like line
<servlet-class>com.sun.jersey.config.property.packages</servlet-class>
in your example should be replaced with
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-cl­ass>

After that I was able to deploy the project. But I still experience issue with accessing REST APIs in browser.
Posted on 1/31/14 9:43 AM in reply to Pradip A Bhatt.
Spring 3 already provides rest support, why we need to use jersey,
any special reason to use jersey instead of spring webservices?
Posted on 2/21/14 3:28 AM.
Have you got this approach to work with Jersey 2 and asynchronous functionality?
Posted on 4/9/14 12:34 PM.
Hi Pradip,

Please tell you have integrated REST API with spring portlet.
I have to integrate it with spring portlet. should I have to follow same steps.

Thanks,
Sneha
Posted on 7/16/14 3:52 AM in reply to Pradip A Bhatt.
thanks for this post. it's helpfull.. But the client program is not very clear :/
Posted on 3/2/15 8:34 AM.