Download presentation
Presentation is loading. Please wait.
1
JDeveloper 11g's REST web services
SAGE Computing Services Customised Oracle Training Workshops and Consulting JDeveloper 11g's REST web services ....A change is as good as a REST Chris Muir Oracle Consultant and Trainer Oracle ACE Director - Fusion Middleware
2
Agenda Part I: ABCs of Web Services
Part II: REST Web Services in a Nutshell Part III: Getting your hands dirty with code
3
Photo thanks to Lexnger@ Flickr.com under CC
Part I: Web Services Photo thanks to Flickr.com under CC
4
What are Web Services? The W3C defines a "web service" as "a software system designed to support interoperable machine-to-machine interaction over a network.” System to system data integration Consumer (client) & Publisher The traditional “Enterprise” view
5
The Traditional “Enterprise” View
Traditional view System to system data integration Consumer (client) & Publisher The contemporary view Images thanks to IconArchive.com – free for non commercial use
6
The “Contemporary” View
Additional new view System to system data integration Consumer (client) & Publisher Key benefits Images thanks to IconArchive.com – free for non commercial use
7
Photo thanks to vernhart@ Flickr.com under CC
Key Benefits? Share data (near realtime) System interoperability Low cost internet delivery Standardized Loosely coupled History Photo thanks to Flickr.com under CC
8
History? RFC-707 A High Level Framework for Network Based Resource Sharing (Remote Procedure Call) Microsoft DCOM Java RMI CORBA SOAP RPC Style SOAP Document Style REST IETF – Internet Engineering Task Fask – ISOC – Internet Society Choice
9
SOAP vs REST Enterprise Ready Programmer Friendly
Supports multiple protocols Contract first Procedural (RPC) based Standardized Maximum security Ideal for SOA support Over engineered Simple solutions become complicated Complex security Payload inefficiency Only over HTTP Verb first (only 4 commands) Simple Familiar Overly simplistic security Not 100% ideal for SOA Little standardization No emphasis on contract Part II – REST Web Services in a Nutshell
10
REST Web Services in a Nutshell
Part II: REST Web Services in a Nutshell What is REST? Photo thanks to Flickr.com under CC
11
A: It stands for Representational State Transfer
Q: What is REST? A: It stands for Representational State Transfer Q: Huh? Can you say that in English? A: Let me explain, do you surf the web? Q: Um, yes I do (I thought I was asking the questions)....? A: Then you already know it. Browser/URL
12
Understand basics of HTTP
13
The Request – HTTP Protocol
GET /about_sage_computing_services.html HTTP/1.1 Host: User-Agent: Mozilla/5.0 Gecko/ Firefox/3.6.10 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO ,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Cookie: __utma= ;; If-Modified-Since: Sun, 31 Jan :35:33 GMT If-None-Match: "1a690fe-40df-f " Cache-Control: max-age=0 Additional new view System to system data integration Consumer (client) & Publisher HTTP HTTP GET/PUT/POST/DELETE GET/PUT/POST/DELETE URIs URIs Accept HTTP Response Images thanks to IconArchive.com – free for non commercial use
14
The Response - HTTP Protocol
HTTP/ OK Date: Sun, 26 Sep :02:25 GMT Server: Apache/ (Ubuntu) Connection: Keep-Alive Keep-Alive: timeout=15, max=96 Etag: "1a690fe-40df-f “ <html xmlns=" lang="en" xml:lang="en“> <head> <title>SAGE Computing Services - About us</title </head> .... Etc .... </html> Additional new view System to system data integration Consumer (client) & Publisher HTTP Error Codes HTTP Error Codes Payload/Body Images thanks to IconArchive.com – free for non commercial use Part II – REST Web Services in a Nutshell
15
REST Web Services in a Nutshell
REST also suits web services Resources – URIs = intuitive by design – gives users & developers ability to anticipate design Based on HTTP verbs – get/put/post/delete = simple to understand Stateless = scalable Support for REST Web Services Photo thanks to Flickr.com under CC
16
Support? JAX-RS Jersey Website
17
Java JAX-RS “Jersey” JAX-RS in detail
18
Java JAX-RS & Jersey JAX-RS Specification (API) Introduced Java SE 5.0
Uses a simplified POJO approach Implements both the client and/or server Jersey is Oracle/Sun’s Reference Implementation Jersey 1.0 – = JAX-RS 1.0 Spec Jersey 1.1+ = JAX-RS 1.2 Spec Oracle JDeveloper 11g
19
Oracle’s JDeveloper 11g Recommended version 11.1.1.3.0+
Many minor issues in earlier versions Jersey supported (latest none beta 1.4) Part III – Getting your hands dirty with code
20
Getting your hands dirty with code
Part III: Getting your hands dirty with code Database “Departments” query Photo thanks to Flickr.com under CC
21
HTML Output/Browser
22
Data is Beautiful
23
Sad iPhone
24
But that’s a HTML page for browsers for people to read
But that’s a HTML page for browsers for people to read. It’s not useful to other computer programs. Data
25
03 <style type="text/css"> 04 table {background-color:#F2F2F5;}
01 <html> 02 <head> 03 <style type="text/css"> 04 table {background-color:#F2F2F5;} 05 td {color:#000000; font-family:Arial; padding:8px; background-color:#F2F2F5;} 06 th {font-family:Arial; font-size:12pt; padding:8px; background-color:#CFE0F1;} 07 </style> 08 </head> 09 <body> 10 <table border="0" cellpadding="0" cellspacing="0"> 11 <tr> <th>DEPARTMENT_ID</th> <th>DEPARTMENT_NAME</th> 14 </tr> 15 <tbody> <tr> <td>10</td> <td>Administration</td> </tr> <tr> <td>20</td> <td>Marketing</td> </tr> <tr> <td>30</td> <td>Purchasing</td> </tr> <tr> <td>40</td> <td>Human Resources</td> </tr> <tr> <td>50</td> <td>Shipping</td> </tr> <tr> <td>60</td> <td>IT</td> </tr> <tr> <td>70</td> <td>Public Relations</td> </tr> <tr> <td>80</td> <td>Sales</td> </tr> <tr> <td>90</td> <td>Executive</td> </tr> <tr> <td>100</td> <td>Finance</td> </tr> 26 </tbody> 27 </table> 28 </body> 29 </html> 30 31 32 HTML page – markup + data
26
03 <style type="text/css"> 04 table {background-color:#F2F2F5;}
01 <html> 02 <head> 03 <style type="text/css"> 04 table {background-color:#F2F2F5;} 05 td {color:#000000; font-family:Arial; padding:8px; background-color:#F2F2F5;} 06 th {font-family:Arial; font-size:12pt; padding:8px; background-color:#CFE0F1;} 07 </style> 08 </head> 09 <body> 10 <table border="0" cellpadding="0" cellspacing="0"> 11 <tr> <th>DEPARTMENT_ID</th> <th>DEPARTMENT_NAME</th> 14 </tr> 15 <tbody> <tr> <td>10</td> <td>Administration</td> </tr> <tr> <td>20</td> <td>Marketing</td> </tr> <tr> <td>30</td> <td>Purchasing</td> </tr> <tr> <td>40</td> <td>Human Resources</td> </tr> <tr> <td>50</td> <td>Shipping</td> </tr> <tr> <td>60</td> <td>IT</td> </tr> <tr> <td>70</td> <td>Public Relations</td> </tr> <tr> <td>80</td> <td>Sales</td> </tr> <tr> <td>90</td> <td>Executive</td> </tr> <tr> <td>100</td> <td>Finance</td> </tr> 26 </tbody> 27 </table> 28 </body> 29 </html> 30 31 32 HTML page - makrup
27
03 <style type="text/css"> 04 table {background-color:#F2F2F5;}
01 <html> 02 <head> 03 <style type="text/css"> 04 table {background-color:#F2F2F5;} 05 td {color:#000000; font-family:Arial; padding:8px; background-color:#F2F2F5;} 06 th {font-family:Arial; font-size:12pt; padding:8px; background-color:#CFE0F1;} 07 </style> 08 </head> 09 <body> 10 <table border="0" cellpadding="0" cellspacing="0"> 11 <tr> <th>DEPARTMENT_ID</th> <th>DEPARTMENT_NAME</th> 14 </tr> 15 <tbody> <tr> <td>10</td> <td>Administration</td> </tr> <tr> <td>20</td> <td>Marketing</td> </tr> <tr> <td>30</td> <td>Purchasing</td> </tr> <tr> <td>40</td> <td>Human Resources</td> </tr> <tr> <td>50</td> <td>Shipping</td> </tr> <tr> <td>60</td> <td>IT</td> </tr> <tr> <td>70</td> <td>Public Relations</td> </tr> <tr> <td>80</td> <td>Sales</td> </tr> <tr> <td>90</td> <td>Executive</td> </tr> <tr> <td>100</td> <td>Finance</td> </tr> 26 </tbody> 27 </table> 28 </body> 29 </html> 30 31 32 Data – 1 per line
28
10 Administration 20 Marketing 30 Purchasing 40 Human Resources 50
Shipping 60 IT 70 Public Relations 80 Sales 90 Executive 100 Finance Data – comma delimiited
29
DEPARTMENT_ID,DEPARTMENT_NAME 10,Administration 20,Marketing
30,Purchasing 40,Human Resources 50,Shipping 60,IT 70,Public Relations 80,Sales 90,Executive 100,Finance Data - XML
30
</DEPARTMENTS>
<DEPT><DEPTID>10</DEPTID><NAME>Administration</NAME></DEPT> <DEPT><DEPTID>20</DEPTID><NAME>Marketing</NAME></DEPT> <DEPT><DEPTID>30</DEPTID><NAME>Purchasing</NAME></DEPT> <DEPT><DEPTID>40</DEPTID><NAME>Human Resources</NAME></DEPT> <DEPT><DEPTID>50</DEPTID><NAME>Shipping</NAME></DEPT> <DEPT><DEPTID>60</DEPTID><NAME>IT</NAME></DEPT> <DEPT><DEPTID>70</DEPTID><NAME>Public Relations</NAME></DEPT> <DEPT><DEPTID>80</DEPTID><NAME>Sales</NAME></DEPT> <DEPT><DEPTID>90</DEPTID><NAME>Executive</NAME></DEPT> <DEPT><DEPTID>100</DEPTID><NAME>Finance</NAME></DEPT> </DEPARTMENTS> Building a REST Web Service
31
Building a REST Web Service
And now for a bit of Java
32
JDBC – Java API for Database Connectivity
And now for a bit of Java JDBC – Java API for Database Connectivity
33
public class DepartmentsResource {
public static String queryDepartmentsSQL() throws SQLException { Connection jdbcConn = String query = "SELECT department_id, department_name FROM departments"; Statement statement = jdbcConn.createStatement(); ResultSet resultSet = statement.executeQuery(query); String result = ""; while (resultSet.next()) { result = result + resultSet.getInt(1) + "\n"; result = result + resultSet.getString(2) + "\n"; } statement.close(); jdbcConn.close(); return result; public static void main(String[] args) throws SQLException { String result = DepartmentsResource.queryDepartmentsSQL(); System.out.println(result); 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 RESTDemo3/Project1
34
RESTDemo3/Project1
35
public class DepartmentsResource {
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Path("/Departments") public class DepartmentsResource { public static String queryDepartmentsSQL() throws SQLException { Connection jdbcConn = String query = "SELECT department_id, department_name FROM departments"; Statement statement = jdbcConn.createStatement(); ResultSet resultSet = statement.executeQuery(query); String result = ""; while (resultSet.next()) { result = result + resultSet.getInt(1) + "\n"; result = result + resultSet.getString(2) + "\n"; } statement.close(); jdbcConn.close(); return result; @GET @Produces("plain/text") public String getDepartments() throws SQLException { return DepartmentsResource.queryDepartmentsSQL(); RESTDemo3/Project2
42
You are now REST certified
Chris Muir Make solution more Object Oriented
43
POJO
44
And now for some more Java
A Plain Old Java Object POJO code
45
public class Department { private int deptId; private String name;
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class Department { private int deptId; private String name; public Department(int newDeptId, String newName) { deptId = newDeptId; name = newName; } public void setDeptId(int newDeptId) { public int getDeptId() { return deptId; public void setName(String newName) { public String getName() { return name; RESTDemo3/Project3
46
public class DepartmentsResource {
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @Path("/Departments") public class DepartmentsResource { public static ArrayList<Department> queryDepartmentsSQL() throws SQLException { Connection jdbcConn = String query = "SELECT department_id, department_name FROM departments"; Statement statement = jdbcConn.createStatement(); ResultSet resultSet = statement.executeQuery(query); ArrayList<Department> departments = new ArrayList<Department>(); while (resultSet.next()) { Department dept = new Department(resultSet.getInt(1), resultSet.getString(2)); departments.add(dept); } statement.close(); jdbcConn.close(); return departments; @GET @Produces("plain/text") public String getDepartments() throws SQLException { String result = ""; for (Department department : DepartmentsResource.queryDepartmentsSQL()) { result = result + department.getDeptId() + "," + department.getName() + "\n"; return result; RESTDemo3/Project3
49
public class DepartmentsResource {
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @Path("/Departments") public class DepartmentsResource { public static Department queryDepartmentSQL(int deptId) throws SQLException { Connection jdbcConn = String query = "SELECT department_id, department_name FROM departments " + "WHERE department_id = ?"; PreparedStatement statement = jdbcConn.prepareStatement(query); statement.setInt(1, deptId); ResultSet resultSet = statement.executeQuery(); Department department = null; if (resultSet.next()) department = new Department(resultSet.getInt(1), resultSet.getString(2)); statement.close(); jdbcConn.close(); return department; } @GET @Path("/{dept_id}") // /Departments/{dept_id} @Produces("plain/text") public String int deptId) throws SQLException { Department department = DepartmentsResource.queryDepartmentSQL(deptId); return department.getDeptId() + "," + department.getName() + "\n"; RESTDemo3/Project4
55
@Produces("plain/text")
@GET @Path("/{dept_id}") @Produces("plain/text") public String int deptId) throws SQLException { Department department = DepartmentsResource.queryDepartmentSQL(deptId); if (department == null) throw new NotFoundException("Department ID " + deptId + " not found"); return department.getDeptId() + "," + department.getName() + "\n"; } 24 25 26 27 28 29 30 31 32 33 34 RESTDemo3/Project5
58
XML?
59
<DEPARTMENTS> <DEPT><DEPTID>10</DEPTID><NAME>Administration</NAME></DEPT> <DEPT><DEPTID>20</DEPTID><NAME>Marketing</NAME></DEPT> <DEPT><DEPTID>30</DEPTID><NAME>Purchasing</NAME></DEPT> <DEPT><DEPTID>40</DEPTID><NAME>Human Resources</NAME></DEPT> <DEPT><DEPTID>50</DEPTID><NAME>Shipping</NAME></DEPT> <DEPT><DEPTID>60</DEPTID><NAME>IT</NAME></DEPT> <DEPT><DEPTID>70</DEPTID><NAME>Public Relations</NAME></DEPT> <DEPT><DEPTID>80</DEPTID><NAME>Sales</NAME></DEPT> <DEPT><DEPTID>90</DEPTID><NAME>Executive</NAME></DEPT> <DEPT><DEPTID>100</DEPTID><NAME>Finance</NAME></DEPT> </DEPARTMENTS>
60
import javax.xml.bind.annotation.*;
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "deptId", "name" }) @XmlRootElement(name = "DEPT") public class Department { @XmlElement(name = "DEPTID", required = true) private int deptId; @XmlElement(name = "NAME", required = true) private String name; public Department() { } // default no-arg constructor required for JAXB public Department(int newDeptId, String newName) { deptId = newDeptId; name = newName; } public void setDeptId(int newDeptId) { deptId = newDeptId; } public int getDeptId() { return deptId; } public void setName(String newName) { name = newName; } public String getName() { return name; } RESTDemo3/Project6
61
// @Path("/Departments") @Produces("plain/text")
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @GET @Produces("plain/text") public String getDepartments() throws SQLException { String result = ""; for (Department department : DepartmentResources.queryDepartmentsSQL()) { result = result + department.getDeptId() + "," + department.getName() + "\n"; } return result; @Path("/{dept_id}") // /Departments/{dept_id} public String int deptId) throws SQLException { Department department = DepartmentResources.queryDepartmentSQL(deptId); if (department == null) throw new NotFoundException("Department ID " + deptId + " not found"); return department.getDeptId() + "," + department.getName() + "\n"; @Produces("application/xml") public Department int deptId) throws SQLException { return department; RESTDemo3/Project6
62
// @Path("/Departments") @Produces("plain/text")
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 @GET @Produces("plain/text") public String getDepartments() throws SQLException { String result = ""; for (Department department : DepartmentsResource.queryDepartmentsSQL()) { result = result + department.getDeptId() + "," + department.getName() + "\n"; } return result; @Path("/{dept_id}") // /Departments/{dept_id} public String int deptId) throws SQLException { Department department = DepartmentsResource.queryDepartmentSQL(deptId); if (department == null) throw new NotFoundException("Department ID " + deptId + " not found"); return department.getDeptId() + "," + department.getName() + "\n"; @Produces("application/xml") public Department int deptId) throws SQLException { return department; RESTDemo3/Project6
66
<DEPARTMENTS> <DEPT><DEPTID>10</DEPTID><NAME>Administration</NAME></DEPT> <DEPT><DEPTID>20</DEPTID><NAME>Marketing</NAME></DEPT> <DEPT><DEPTID>30</DEPTID><NAME>Purchasing</NAME></DEPT> <DEPT><DEPTID>40</DEPTID><NAME>Human Resources</NAME></DEPT> <DEPT><DEPTID>50</DEPTID><NAME>Shipping</NAME></DEPT> <DEPT><DEPTID>60</DEPTID><NAME>IT</NAME></DEPT> <DEPT><DEPTID>70</DEPTID><NAME>Public Relations</NAME></DEPT> <DEPT><DEPTID>80</DEPTID><NAME>Sales</NAME></DEPT> <DEPT><DEPTID>90</DEPTID><NAME>Executive</NAME></DEPT> <DEPT><DEPTID>100</DEPTID><NAME>Finance</NAME></DEPT> </DEPARTMENTS>
67
@XmlAccessorType(XmlAccessType.FIELD)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "departments" }) @XmlRootElement(name = "DEPARTMENTS") public class Departments { @XmlElement(name = "DEPT", required = true) protected List<Department> departments; public List<Department> getDepartments() { if (departments == null) { departments = new ArrayList<Department>(); } return this.departments; RESTDemo3/Project7
68
public static Departments queryDepartmentsSQL() throws SQLException {
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public static Departments queryDepartmentsSQL() throws SQLException { Connection jdbcConn = String query = "SELECT department_id, department_name FROM departments"; Statement statement = jdbcConn.createStatement(); ResultSet resultSet = statement.executeQuery(query); Departments departments = new Departments(); List<Department> departmentList = departments.getDepartments(); while (resultSet.next()) { Department dept = new Department(resultSet.getInt(1), resultSet.getString(2)); departmentList.add(dept); } statement.close(); jdbcConn.close(); return departments; @GET @Produces("application/xml") public Departments getDepartmentsXML() throws SQLException { return DepartmentsResource.queryDepartmentsSQL(); RESTDemo3/Project7
71
public static void insertDepartmentSQL(int deptId, String name)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public static void insertDepartmentSQL(int deptId, String name) throws SQLException { Connection jdbcConn = String insert = "INSERT INTO departments (department_id, department_name) " + "VALUES (?, ?)"; PreparedStatement statement = jdbcConn.prepareStatement(insert); statement.setInt(1, deptId); statement.setString(2, name); boolean result = statement.execute(); jdbcConn.commit(); statement.close(); jdbcConn.close(); } @POST @Path("/{dept_id}") public void int deptId String name) DepartmentsResource.insertDepartmentSQL(deptId, name); RESTDemo3/Project8
74
public static void updateDepartmentSQL(int deptId, String name)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public static void updateDepartmentSQL(int deptId, String name) throws SQLException { Connection jdbcConn = String update = "UPDATE departments SET department_name = ? " + "WHERE department_id = ?"; PreparedStatement statement = jdbcConn.prepareStatement(update); statement.setString(1, name); statement.setInt(2, deptId); boolean result = statement.execute(); jdbcConn.commit(); statement.close(); jdbcConn.close(); } @PUT @Path("/{dept_id}") public void int deptId String name) DepartmentsResource.updateDepartmentSQL(deptId, name); RESTDemo3/Project8
77
public static void deleteDepartmentSQL(int deptId)
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public static void deleteDepartmentSQL(int deptId) throws SQLException { Connection jdbcConn = String delete = "DELETE departments WHERE department_id = ?"; PreparedStatement statement = jdbcConn.prepareStatement(delete); statement.setInt(1, deptId); boolean result = statement.execute(); jdbcConn.commit(); statement.close(); jdbcConn.close(); } @DELETE @Path("/{dept_id}") public void int deptId) DepartmentsResource.deleteDepartmentSQL(deptId); RESTDemo3/Project8
80
(Part IV The stuff I didn’t cover)
HATEOAS Security JSON WADL Designing REST web services
81
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction.“ -- Albert Einstein
82
Questions and Answers? SAGE Computing Services
Customised Oracle Training Workshops and Consulting Questions and Answers? Presentations are available from our website: Twitter: chriscmuir Linkedin:
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.