WebServices

Web Services

RESTful Web Services are REST architecture . RESTful web services are highly scalable . REST means  REpresentational State Transfer . it uses HTTP protocol Following HTTP methods are used in REST based architecture.

  • GET – Provides a read only access to a resource. ( SELECT records )
  • PUT – Used to create a new resource. ( INSERT records )
  • DELETE – Used to remove a resource. ( DELETE records )
  • POST – Used to update a existing resource or create a new resource. ( UPDATE)
  • OPTIONS – Used to get the supported operations on a resource. ( LIST )

Create REST Webservices using JDeveloper 12c

Create New ADF Application , Import Emp.java which basically has getter and setter methods for Data Model Emp, and also one method to convert string to a single XML as shown below [code] package model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Emp { public Emp() { super(); } private String name; private int id; private int exp; private int salary; public Emp(String name, int id, int exp, int salary) { super(); this.name = name; this.id = id; this.exp = exp; this.salary = salary; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setExp(int exp) { this.exp = exp; } public int getExp() { return exp; } public void setSalary(int salary) { this.salary = salary; } public int getSalary() { return salary; } public String toString() { String empXml = “<name>”+getName()+”</name>”+”\n”+ “<id>”+getId()+”</id>”+”\n”+ “<experience>”+getExp()+”</experience>”+”\n”+ “<salary>”+getSalary()+”</salary>”; return empXml; } } [/code] The Service Class will have actual Implementation to call REST Services [code] package model; import java.util.*; import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path(“/restlab”) public class Service { // Singleton Collection static ArrayList<Emp> loe = new ArrayList<Emp>(); public Service() { super(); } //Create public void addEmp (@QueryParam(“id”) int id, @QueryParam(“exp”)int exp, @QueryParam(“name”) String name, @QueryParam(“sal”)int sal) { Emp e = newEmp(exp,name,sal,id); loe.add(e); } @GET @Produces(“application/xml”) //Read public ArrayList<Emp> getEmps() { return loe; } //Update public void upadteEmp(int delId, int id, int exp, String name, int sal) { deleteEmp(delId); addEmp(id,exp,name,sal); } @DELETE //Delete public void deleteEmp(@QueryParam(“id”) int id) { loe.remove(getObj(id)); } //Helper Methods @PUT @Path(“defaultEmp”) public Response addEmp(){ loe.add(new Emp(“Adderley”,1,5,1000)); loe.add(new Emp(“Coltrane”,2,6,2000)); loe.add(new Emp(“Davis”,3,7,3000)); return Response.ok().build(); } Emp newEmp(int exp, String name, int sal, int id){ return new Emp(name,id,exp,sal); } public Emp getObj(int id) { Iterator i = loe.iterator(); while(i.hasNext()){ Emp emp = (Emp)i.next(); if((emp.getId())==id) { System.out.println(emp.getName()); return emp; } } return null; } } [/code] if you come across Complilation Errors then Click the light bulb in the margin and select the Configure Project for JAX-RS Web Services option. this will auto generate GenericApplication.java y10105c [code] package model; import java.util.HashSet; import java.util.Set; import javax.ws.rs.ApplicationPath; @ApplicationPath(“resources”) public class GenericApplication extends javax.ws.rs.core.Application { public Set<Class<?>> getClasses() { Set<Class<?>> classes = new HashSet<Class<?>>(); // Register root resources. // Register provider classes. return classes; } } [/code]

Testing the REST WEB Service

Right Click on the Service and Select Test Web Service, we should be able to see GET in HTTP Analyser On URL : http://localhost:7101/RestWSApp-Model-context-root/resources/restlab getEmps GET will return an XML of all the records as shown below jdev You can also use DELETE Operation to remove a record based on ID Example : http://localhost:7101/RestWSApp-Model-context-root/resources/restlab?id=7 del1 Use GET Operation to view remaining records show1

POJO based Web Services

to create a POJO based WebService @WebService(serviceName = “MyCompanyWS”) is used to declare the service name and @WebMethod Annotation is used to declare a method that can be exposed as a WebService [code] package annotation; import java.util.ArrayList; import java.util.Collection; import javax.jws.WebMethod; import javax.jws.WebService; @WebService(serviceName = “MyCompanyWS”) public class MyCompany { public MyCompany() { Collection<Dept> depts = new ArrayList<Dept>(); Collection<Emp> myEmp10 = new ArrayList<Emp>(); Emp a = new Emp(1,”Larry”); myEmp10.add(a); a = new Emp(2,”Ken”); myEmp10.add(a); depts.add(new Dept(10,”Administration”,”Redwood City”,myEmp10)); Collection<Emp> myEmp20 = new ArrayList<Emp>(); a = new Emp(3,”Gary”); myEmp20.add(a); a = new Emp(4,”Shay”); myEmp20.add(a); a = new Emp(5,”Duncan”); myEmp20.add(a); depts.add(new Dept(20,”JDeveloper”,”Redwood Shores”,myEmp20)); Collection<Emp> myEmp30 = new ArrayList<Emp>(); a = new Emp(8,”Mike”); myEmp30.add(a); a = new Emp(7,”George”); myEmp30.add(a); a = new Emp(6,”David”); myEmp30.add(a); depts.add(new Dept(30,”WebLogic”,”New York”,myEmp30)); this.setMyDepts(depts); } Collection<Dept> myDepts = new ArrayList<Dept>(); public void setMyDepts(Collection<Dept> myDepts) { this.myDepts = myDepts; } public Collection<Dept> getMyDepts() { return myDepts; } public boolean addEmployeeToDept(Emp emp, int deptid){ //TODO write some logic here System.out.println(“Here we’ll be adding an employee to ” +deptid); return true; } //@WebMethod public Dept getDeptInfo (int id) { for (Dept a: this. getMyDepts() ) { if (a.getId() == id) { return a; } } return null; } } [/code]

Supporting Java Beans

[code] package annotation; public class Emp { public Emp() { } int id; String name; float salary; public Emp(int id, String name) { super(); this.id = id; this.name = name; } public void giveRaise(float moreMoney){ this.setSalary(this.getSalary()+moreMoney); } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setSalary(float salary) { this.salary = salary; } public float getSalary() { return salary; } } [/code] and [code] package annotation; import java.util.Collection; public class Dept { public Dept() { } int id; String name; String location; Collection<Emp> employees; public Dept(int id, String name, String location, Collection<Emp> employees) { super(); this.id = id; this.name = name; this.location = location; this.employees = employees; } public void setId(int id) { this.id = id; } public int getId() { return id; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setLocation(String location) { this.location = location; } public String getLocation() { return location; } public void setEmployees(Collection<Emp> employees) { this.employees = employees; } public Collection<Emp> getEmployees() { return employees; } } [/code]

Web Services

pojows

REFER

11g JDeveloper Rest WS : https://docs.oracle.com/cd/E18941_01/tutorials/jdtut_11r2_53/jdtut_11r2_53.html 11g POJO based WS : http://www.oracle.com/webfolder/technetwork/tutorials/obe/jdev/obe11jdev/ps1/webservices/ws.html