Monday, May 12, 2014

Develop and Deploy Jersey RESTful service using JDeveloper in Weblogic

Jersey is one of reference implementations of JAX-WS specification which is to describe how REST should be implemented using annotation-driven POJO in Java. Jersey is the one that is most widely used in development and production. WebLogic Server ships with a set of pre-built shared libraries, packaged as Web applications. Jesery is one of these and it is required to run applications that are based on the Jersey JAX-RS RI. The version of Jsersey in weblogic 10.3 is 1.9 which implements JAX-RS 1.1.

This jersey war file is located in:  $Middleware/wlserver_10.3/common/deployable-libraries/jersey-bundle-1.9.war.


Register Jersey as the shared library in Weblogic server 


Before the REST application can be deployed and run on Weblogic the Jersey JAX-RS RI shared libraries needs to be registered in Weblogic Servcre instance. It can be done via Weblogic Console. 
Login into Weblogic Console Click on Deployments in Domain Structure
Click on Lock & Edit button to enable Install button and then click on Install button


The path should be:  /u01/app/fmwsodev/Middleware/wlserver_10.3/common/deployable-libraries
Select jersey-bundle-1.9.war and click on Next button

Select Install this deployment as a library and click on Next button

Select target and click on Next button


Select target and click on Next button


Confirm the library name, version and implementation version as below and click on Finish button



Now Jersey JAX-RS RI is registered as shared library in Weblogic server.  Any application deployed on the server can use this library for implementation of REST service using Jersey.



Develop Jersey REST service using JDeveloper


Create a generic project in JDeveloper




In order to develop Java codes which use Jersey to implement RESTful service, two jar files are needed to add into User Managed Libraries in JDeveloper.


First jar is jersey-bundle-1.9.jar which can be downloaded from:

http://repo1.maven.org/maven2/com/sun/jersey/jersey-bundle/1.9/jersey-bundle-1.9.jar


Since jersey-bundle-1.9.jar doesn't include JAX-RS API classes.  So JAX-RS API jar is also needed.   The jar file can be found from Weblogic server installation folder: $Middleware/modules/javax.ws.rs_1.0.0.0_1-1-1.jar


After two jar files are added into the libraries use these two libaries in the project: RESTProject by using Add Library in Libraries and Classpath from Project Properties... menu item.


Now create a POJO which will implement the service:

The source codes of Java class: RESTService are as below:


package com.toic.rest;

import com.toic.model.Folder;

import java.util.logging.Logger;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("/folders")
public class RESTService {
    
    private Logger logger = Logger.getLogger(RESTService.class.getName());
    
    public RESTService() {
        super();
    }
    
    @GET
    @Path("/guid/{folderguid}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getFoldersByGUID(@PathParam("folderguid") String folderguid) {
        
        Folder folder = Folder.makeFolders();
        
        return Response.status(Response.Status.OK).entity(folder).build();
    }
}
The below is one domain class Folder which is used in RESTService class.


package com.toic.model;

import java.util.ArrayList;
import java.util.List;

public class Folder {
    
    private String folderName;
    
    private String owner;
    
    private String folderGUID;
    
    private String folderType;
    
    private List childFolders = new ArrayList();
    
    
    public Folder() {
        super();
    }

    public String getFolderName() {
        return folderName;
    }

    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getFolderGUID() {
        return folderGUID;
    }

    public void setFolderGUID(String folderGUID) {
        this.folderGUID = folderGUID;
    }

    public String getFolderType() {
        return folderType;
    }

    public void setFolderType(String folderType) {
        this.folderType = folderType;
    }

    public List getChildFolders() {
        return childFolders;
    }

    public void addChildFolder(Folder childFolder) {
        this.childFolders.add(childFolder);
    }
    
    public static Folder makeFolders() {
        Folder rootFolder = new Folder();
        
        rootFolder.setFolderName("Root Folder");
        rootFolder.setOwner("Mark");
        rootFolder.setFolderGUID("123456");
        rootFolder.setFolderType("NATIVE");
        
        Folder childFolder1 = new Folder();
        
        childFolder1.setFolderName("Folder1");
        childFolder1.setOwner("Mark");
        childFolder1.setFolderGUID("100234");
        childFolder1.setFolderType("NATIVE");
        
        Folder childFolder2 = new Folder();
        
        childFolder2.setFolderName("Folder2");
        childFolder2.setOwner("Mark");
        childFolder2.setFolderGUID("200234");
        childFolder2.setFolderType("NATIVE");

        rootFolder.addChildFolder(childFolder1);
        
        rootFolder.addChildFolder(childFolder2);
        
        return rootFolder;
    }
}


In RESTService.java editor click on Quick Fix to configure web.xml for this service.



web.xml is created under WEB-INF.


Update url-pattern to *


Then create Weblogic Deployment Descriptor


weblogic.xml file is created under WEB-INF as shown as below:


In run time the deployed service will use the shared library of Weblogic.  Here in weblogic.xml the library reference should be created as below:


Next step is to update the deployment profile of the service which is deployed as web application,


The service is called RESTService.


Update the context root as: Demo.



Deploy Jersey REST service and test the service


After all is done the service is ready to deploy to Weblogic server from JDeveloper.



If the deployment is successful RESTService will appear in the deployments of Weblogic Console.


Click on RESTService and click on Testing tab



Click on the Test Point URL to start browser and type in the url as: http://weblogicserverhost:8001/Demo/folders/guid/123456 and the service will respond as below:

















3 comments:

  1. Mark, Thanks for the effort with this post. I ran into an issue deploying a rest appplication to a wls 10.3.6 server and the need to install the jax library as well as include it in my weblogic.xml escaped me. Thanks for the help

    ReplyDelete
  2. Mark,

    What changes shall I do in web.xml and weblogic.xml to override loading of jersey and jax-rs jars from project web-inf/lib folder?

    ReplyDelete
  3. yeah!! Send SMS Attachment solutions and functionalities to the high engagement mass text messaging campaigns of small and large businesses. This has enabled business organizations to share files, brochures, and menus through SMS just the way they did it with an email. Users get the flexibility of attaching files to their bulk messages.

    ReplyDelete