Wednesday, September 10, 2014

REST service on weblogic 10.3.4

In this blog we will be developing a simple REST service using jdev and deploying it on weblogic 10.3.4.


NOTE: Jdeveloper 12c has better support for REST service creation but in this blog I am using Jdev 11.1.2.4 (or any older version) because I have to deploy these services on weblogic 10.3.4

NOTE: We will be using Jersey reference implementation of jsr311 to create REST services

We will perform following steps
1. Configure weblogic 10.3.4 with Jersey libraries
2. Create a REST service
3. Configure web.xml for REST service resources
4. Configure weblogic.xml to refer shared Jersey libraries
5. Deploy project on weblogic server
6. Access REST service

==================================================================
1. Configure weblogic 10.3.4 with Jersey libraries:

We get Jersey libraries with weblogic but they are not by default installed so we need to install them ourself.
Jersey libraries are available at $WL_HOME/common/deployable-libraries
Install jersey-bundle-1.1.5.1.war, jsr311-api-1.1.1.war on weblogic server as a Shared libraries.
Follow these steps for installing libraries (one at a time)
a. Launch weblogic console http://<server>:<port>/console
b. Navigate to Deployments
c. Click on Install
    
d. Select jersey-bundle-1.1.5.war
    
e. Select 'Install this deployment as library'
   
f. Select server on which you want to deploy this library. Note you can deploy rest service on same server. I am selecting 'Admin Server' so while deploying I can deploy REST service only on 'Admin server'. Admin server serves request from port 7001, which I need to use while accessing service.
  
f. Click Next Next and finish on rest of the pages.


Similarly install jsr311-api.war


2. Create a REST service

Next we need to create a simple REST service
Follow these steps to create REST service

a. Create an application/project.
    If you don't have an application/project already, create it.
    Create custom application/project.

    

   


b. Add Jersey library to project
     


c. Create a new java class to represent a resource
    REST stands for 'Representational State Transfer'. Actually we are transferring a resource between client and server. Every resource can be represented by structure (XML, HTML, TEXT, JSON) etc. We just want to transfer that representation of resource using REST service.

   Create a java class as shown below
   package com.san.rest.service;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


@Path("helloworld")
public class HelloWorld {

  // To generate plain text
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String helloInPlainText() {
    return "Hello! How are you?";
  }

  // To generate xml
  @GET
  @Produces(MediaType.TEXT_XML)
  public String helloInXML() {
    return "<?xml version=\"1.0\"?>" + "<welcomeMsg> Hello! How are you?" + "</welcomeMsg>";
  }

  // To generate html
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String helloInHTML() {
    return "<html> " + "<title>" + "Hello" + "</title>"
        + "<body><h1>" + "How are you?" + "</body></h1>" + "</html> ";
  }

}

3. Configure web.xml for REST service resources
  
a. Create web.xml (Java EE deployment descriptor) for your project.
     New > Search for 'Java EE Deployment Descriptor'
     Follow wizard
    

b. Add following entry in web.xml file
NOTE: There are other ways also to specify resource classes to jersey. For example using init parameter javax.ws.rs.Application, but I believe using com.sun.jersey.config.property.packages parameter is easiest way. 

NOTE: If you have service resource in multiple packages specify value as 
  <param-value>pkg1;pkg2;pkg3</param-value>

4. Configure weblogic.xml to refer shared Jersey libraries

   a. Create weblogic deployment descriptor (weblogic.xml) file
       New > Search for 'Weblogic Deployment Descriptor'

    



b. Add shared libraries in weblogic.xml file


5. Deploy project on weblogic server

a. Set context root for your project
 b. Create WAR deployment profile
   Project Properties > Deployment Profile > New (War deployment profile)
  

c. Deploy project on weblogic server
     Now deploy project on weblogic server as normal war file.
      You can create app server connection in resource palate and then right click on project and deploy
      OR alternatively you can deploy war file first and then deploy it using weblogic console

 
6. Access REST service 

You can access service as http://<server>:<port>/<context-root>/<url-pattern>/<path>

server: weblogic server host
port: weblogic server port
context-root: defined in step 5. myrestservice in our case
url-pattern: defined in web.xml. /rest in our case
path: defined as @Path annotation in service class. helloworld in our case



Disclaimer: Any views or opinions presented in this blog are solely those of the author and do not necessarily represent those of the company.