Integrate Jersey RESTful with portlet project

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
Blogs
Awesome dude... you rock emoticon
Really???

Is this become helpful to you???
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.
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.
ca you please answer my question posted in http://stackoverflow.com/questions/18832785/is-a-servlet-inside-a-portlet-required-to-make-the-portlet-as-a-web-service

related to your above post.
ca you please answer my question posted in http://stackoverflow.com/questions/18832785/is-a-servlet-inside-a-portlet-required-to-make-the-portlet-as-a-web-service

related to your above post.
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:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.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?
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/
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-class>

After that I was able to deploy the project. But I still experience issue with accessing REST APIs in browser.
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
Spring 3 already provides rest support, why we need to use jersey,
any special reason to use jersey instead of spring webservices?
Have you got this approach to work with Jersey 2 and asynchronous functionality?
thanks for this post. it's helpfull.. But the client program is not very clear :/