Monday, September 15, 2014

REST: Adding JSON support using jackson on weblogic 10.3.4

In my previous blog I showed how to create a RESTful service on weblogic 10.3.4

In this blog I am going to show how to provide JSON response from webservice using jackson apis.

jackson libraries are used to convert a JSON message into a java object and also java object to a JSON message.

We need to follow these steps
1. Configure weblogic for jackson libraries
2. Add jackson libraries to jdev project
3. Write pojos which needs to be exposed as JSON
4. Write service method and configure web.xml with Jersey servlet
5.  Deploy project on weblogic
6. Test

1. Configure weblogic for jackson libraries

 Similarly as in previous blog we have added jersey libraries as a shared libraries in weblogic, this time we need to add jackson libraries. These libraries are available under $WL_HOME/common/deployable-libraries
 We need to add jackson-core-asl, jackson-jaxrs, jackson-mapper-asl libraries. For detail steps about how to add libraries see blog ''

2. Add jackson libraries to jdev project

 For design time support of jackson libraries we need to add jackson libraries to our project.
 Follow these steps
a. Navigate to project properties > Library and classpath
b. Add Library 'JAX-RS Jersey Jacson (Bundle)' as shown below

For runtime support configure weblogic.xml so that code can refer to shared libraries of weblogic that we have installed in first step.

3. Write POJOs which needs to be exposed as JSON

As I said earlier jackson libraries can be used to convert java objects to JSON message and vice versa. We need to create POJO objects which needs to be represented in JSON. In this example I am creating employee and address pojos as shown below
import java.util.List;
public class Employee {
    private Long empId;
    private String name;
    private List addresses;   
    public Employee() {

    public void setEmpId(Long empId) {
        this.empId = empId;
    public Long getEmpId() {
        return empId;
    public void setName(String name) { = name;
    public String getName() {
        return name;
    public void setAddresses(List addresses) {
        this.addresses = addresses;
    public List getAddresses() {
        return addresses;

public class Address {
    private Long addressId;
    private String addressType;
    private String line1;
    private String line2;
    private String city;
    private String state;
    private String country;
    private String zipCode;
    public Address() {

    public void setAddressId(Long addressId) {
        this.addressId = addressId;

    public Long getAddressId() {
        return addressId;

    public void setAddressType(String addressType) {
        this.addressType = addressType;

    public String getAddressType() {
        return addressType;

    public void setLine1(String line1) {
        this.line1 = line1;

    public String getLine1() {
        return line1;

    public void setLine2(String line2) {
        this.line2 = line2;

    public String getLine2() {
        return line2;

    public void setCity(String city) { = city;

    public String getCity() {
        return city;

    public void setState(String state) {
        this.state = state;

    public String getState() {
        return state;

    public void setCountry(String country) { = country;

    public String getCountry() {
        return country;

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;

    public String getZipCode() {
        return zipCode;

NOTE: There is a one to many relationship in these two classes. One employee can have multiple addresses. We have included an instance variable addresses of type List in Employee class.

4. Write service method and configure web.xml with Jersey servlet

 Create new class EmployeeService in package
 NOTE: This package is already configured to have Jersey service classes in previous blog. It is done by creating entry in web.xml. See previous blog

import java.util.ArrayList;
import java.util.List;
import org.codehaus.jackson.JsonGenerationException;

public class EmployeeService {
    public EmployeeService() {
    // To generate json
    public String getEmployeeDetails() {
        Address a1 = new Address();
        a1.setLine1("Flat 1 ABC appartment");
        a1.setLine2("3rd Cross 1st Mian, MG Area");
        Address a2 = new Address();
        a2.setLine1("Flat 2 XYZ appartment");
        a2.setLine2("9th Cross 1st Mian, MG Area");
        List addrList = new ArrayList();
        Employee e1 = new Employee();
        StringWriter output = new StringWriter();                                          
        ObjectMapper mapper = new ObjectMapper();
        try {
            mapper.writeValue(output, e1);
        } catch (JsonGenerationException e) {
            throw new RuntimeException("JsonGenerationException: " + e.getMessage());       
        } catch (JsonMappingException e) {
            throw new RuntimeException("JsonMappingException: " + e.getMessage());
        } catch (IOException e) {
            throw new RuntimeException("IOException: " + e.getMessage());
        return output.toString();


Important parts of this class are
a. Import of jackson classes: These classes are available from jackson libraries that we have added in project.
b. @Path("employee"): It means we need to add /employee in our url while calling this service
c. @GET: It means a get service
d. @Produces(MediaType.APPLICATION_JSON): It means response is of type JSON
e. Populate Employee/Address objects: As this is just a demo to show JSON response, I have created employee/address object inside service method itself but in general I would like to call a utility method or EJB session bean to provide me data about Employee/Address objects
f.  mapper.writeValue(output, e1): This method converts employee object e1 into its JSON representation.

Once again we can see our web.xml entry
        <display-name>Rest Jersey Application</display-name>


5.  Deploy project on weblogic
 Create a war profile and deploy it on weblogic server. Note down context root while creating war profile. Also explained in previous blog

6. Test

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

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 (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-, 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

public class HelloWorld {

  // To generate plain text
  public String helloInPlainText() {
    return "Hello! How are you?";

  // To generate xml
  public String helloInXML() {
    return "<?xml version=\"1.0\"?>" + "<welcomeMsg> Hello! How are you?" + "</welcomeMsg>";

  // To generate 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, but I believe using parameter is easiest way. 

NOTE: If you have service resource in multiple packages specify value as 

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.