Home > OSB > OSB11G calling RESTful Web Service Example

OSB11G calling RESTful Web Service Example

In this post will see how the RESTful Web Service can be invoked from OSB11G Proxy Service with dynamic relative-URI
This RESTfull service respond with JSON message format. Used one JavaCallout to convert the JSON message into XML

1.RESTful Web Service
Here retrieving employee details by passing name is exposed as RESTful web service by deploying it on Tomcat server.
Below is the code used for building this simple RESTful service



Employee.java

package com.wordpress.rathinasaba.RESTws;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="employee")
public class Employee implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
    private Long id;
    private String name;
    private String dept;
 
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }

}



EmployeeResource

package com.wordpress.rathinasaba.RESTws;

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

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/services/emp")
public class EmployeeResource{

	private Employee emp1, emp2,emp3,emp4;

	public EmployeeResource() {
		emp1 = new Employee();
		emp1.setId(Long.valueOf(100));
		emp1.setName("Rathina");
		emp1.setDept("ResearchDevelopment");

		emp2 = new Employee();
		emp2.setId(Long.valueOf(101));
		emp2.setName("Sabapathy");
		emp2.setDept("InnovationQuality");
		
		emp3 = new Employee();
		emp3.setId(Long.valueOf(102));
		emp3.setName("Ganesh");
		emp3.setDept("Development");

		emp4 = new Employee();
		emp4.setId(Long.valueOf(103));
		emp4.setName("Praveen");
		emp4.setDept("Testing");
	}

		
	@GET
	@Path("/{name}")
	@Produces("application/json")
	public List<Employee> getEmployeeByName(@PathParam("name") String name){
		List<Employee> coll=new ArrayList<Employee>();
		List<Employee> all=getEmpList();
		if("all".equalsIgnoreCase(name)){
			return all;
		}else{
			for(Employee empObj:all){
				if(empObj.getName().equalsIgnoreCase(name.trim())){
					coll.add(empObj);
				}
			}
			return coll;
		}
	}
	
	private List<Employee> getEmpList(){
		List<Employee> empList = new ArrayList<Employee>();
		empList.add(emp1);
		empList.add(emp2);
		empList.add(emp3);
		empList.add(emp4);
		return empList;
	}

}



web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>EmployeeREST</display-name>
	<servlet>
		<servlet-name>RESTWS</servlet-name>
		<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
		<init-param>
		     <param-name>com.sun.jersey.config.property.packages</param-name>
		     <param-value>com.wordpress.rathinasaba.RESTws</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>RESTWS</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>



OSB11G with RESTfull service
1. My Project structure will look like the shown below.

ProjectStructure

ProjectStructure





2. Complete flow will look like the below screenshot

CompleteOSB11G_RESTful

CompleteOSB11G_RESTful





2. Created a Proxy Service with Message Type as “Any XML Service” along with the below settings

PS_Employee_REST_config

PS_Employee_REST_config





3. Created a Business Service with the deployed RESTful Web Service URI. Below shown is the Business Service configuration
3.1. Selected the Message Type as “Any XML Service”
3.2. Request Message Type and Response Message Type as “None” and “Text” correspondingly
3.3. HTTP Request Method as “GET”



Below is the Business Service “BS_Employee_REST.biz” configuration

BS_Employee_REST_Config

BS_Employee_REST_Config





4. The consumer request will be look like the below given XML. In the request pipeline, getting the value of “employee/name” from input xml and assigning it on variable “name”

<employee>
	<name>rathina</name>
</employee>
AssignVariable_Name

AssignVariable_Name



5. Next in Insert activity , forming the transport header value and assigning it on inbound variable which is passed as input of Business Service as shown below.

dynamicRelativeURI

dynamicRelativeURI




6. Added Routing inside Route.
6.1. In the first insert activity forming complete URI for RESTful service by appending relative-URI along with Business Service URI and assigning to transport header outbound variable of Business Service as shown below

Business Service Endpoint URI :http://localhost:8090/EmployeeREST/services/emp
Relative URI received in the inbound variable of Business Service: {$name}

Form_REST_URI

Form_REST_URI




6.2. In the next insert activity http-method is set and assigning to transport header outbound variable as shown

SetHttpMethod

SetHttpMethod





7. In the response processing pipeline, the JSON response received from RESTful Web Service is passed to Java utility to convert into XML and using the replace activity this response XML is set into response body varibale as an output of this OSB Proxy service.

ResponseJavaCallout

ResponseJavaCallout


AssignResponseToBody

AssignResponseToBody





8. Below is the Java class used to convert the JSON message into XML

package com.wordpress.rathinasaba.utility;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;

public class JsonXmlManager {
	
	public static String jsonToXml(String jsonResponse, String rootName) {
		String xml = null;
		XMLSerializer serializer = new XMLSerializer();
		JSON json = JSONSerializer.toJSON(jsonResponse);
		serializer.setRootName(rootName);
		serializer.setTypeHintsEnabled(false);
		xml = serializer.write(json);
		System.out.println(xml);
		return xml;
	}
	
	public static void main(String[] args) {
		String json="{\"employee\":[{\"dept\":\"ResearchDevelopment\",\"id\":\"100\",\"name\":\"Rathina\"}]}";
		
		System.out.println("JSON to XML Response :"+jsonToXml(json, "root"));
		
	}
	
}



8.1. Below are the supporting jars should be placed in the Server classpath
Jar location in Server : {FMW_HOME}/user_projects\domains\osb_domain\lib
List of jars:
a. commons-beanutils-1.7.0.jar
b. commons-lang-2.6.jar
c. commons-collections-3.2.jar
d. commons-logging-1.1.1.jar
e. ezmorph-1.0.6.jar
f. json-lib-2.4-jdk15.jar
g. xom-1.2.10.jar

9. Test OSB11G_RESTfulService

Testing

Testing


Download Json to xml converter supporting jars from here

Download the JSONtoXMLConverter jar from here

Download the RESTful Web Service source from here

Download OSB11_RESTfulService source from here

Advertisements
Categories: OSB Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: