provide-free-support Ujjwal's Blog: Integrate Jersey RESTful with portlet project

Friday, July 5, 2013

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

No comments:

best-it-exam-    | for-our-work-    | hottst-on-sale-    | it-sale-    | tast-dumps-us-    | test-king-number-    | pass-do-it-    | just-do-it-    | pass-with-us-    | passresults-everything-    | passtutor-our-dumps-    | realtests-us-exam-    | latest-update-source-for-    | cbtnuggets-sale-exam    | experts-revised-exam    | certguide-sale-exam    | test4-sale-exam    | get-well-prepared-    | certkiller-sale-exam    | buy-discount-dumps    | how-to-get-prepared-for-the    | in-an-easy-way    | brain-dumps-sale    | with-pass-exam-guarantee    | accurate-study-material    | at-first-try    | 100%-successful-rate    | get-certification-easily    | material-provider-exam    | real-exam-practice    | with-pass-score-guarantee    | certification-material-provider    | for-certification-professionals    | get-your-certification-successfully    | 100%-Pass-Rate    | in-pdf-file    | practice-exam-for    | it-study-guides    | study-material-sku    | study-guide-pdf    | prep-guide-demo    | certification-material-id    | actual-tests-demo    | brain-demos-test    | best-pdf-download    | our-certification-material    | best-practice-test    | leading-provider-on    | this-course-is-about    | the-most-reliable    | high-pass-rate-of    | high-pass-rate-demo    | recenty-updated-key    | only-for-students-free-download    | courseware-plus-kit-for    | accurate-answers-of    | the-most-reliable-id    | provide-training-for    | welcome-to-buy    | material-for-success-pass    | provide-free-support    | best-book-for-pass    | accuracy-of-the-answers    | pass-guarantee-id    | pass-exam-key    | pass-sku-id    | study-for-exid    | pdf-sku-exid    | sku-pdf-id    | pdf-demo-key    | exam-demo-ske    | pass-it-dump    |
http://www.menuiserie-dauvergne.com/    | http://www.menuiserie-dauvergne.com/    |