0% found this document useful (0 votes)
179 views

Slide 4 - Basic RESTful API

(1) The document discusses REST APIs and various ways to consume REST APIs including with AngularJS, Java, and RestTemplate. (2) It provides examples of consuming the Firebase REST API with Postman and explains operations like GET, POST, PUT, and DELETE. (3) Frameworks like AngularJS, Java, and Spring make consuming REST APIs easier through services like $http and RestTemplate that handle REST calls.

Uploaded by

Nguyễn Thái
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views

Slide 4 - Basic RESTful API

(1) The document discusses REST APIs and various ways to consume REST APIs including with AngularJS, Java, and RestTemplate. (2) It provides examples of consuming the Firebase REST API with Postman and explains operations like GET, POST, PUT, and DELETE. (3) Frameworks like AngularJS, Java, and Spring make consuming REST APIs easier through services like $http and RestTemplate that handle REST calls.

Uploaded by

Nguyễn Thái
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Conceive Design Implement Operate

BASIC RESTFUL API


GIẢNG VIÊN: NGUYỄN NGHIỆM
AGENDA
WEB SERVICE

Web
Application
Web Service

Desktop o1 o2

Consume
Application
o3 DB

Moble o4 oN
Application

Web SOAP
Web Server
Service

Web service operations là các phương


Web Service Consumers thức cho phép các ứng dụng sử dụng qua web
COLLABORATION MODEL

Consume WS1 WS3


Consume
Application

Consume WS2 WS4


Consume
REST & REST API
REST (Representational State Transfer) là các quy ước biểu diễn
dữ liệu chuyển đổi giữa các ứng dụng.
REST API (Application Programming Interface) (còn gọi là RESTful
API) là Web Service hoạt động theo các tiêu chuẩn:
Operations: GET, POST, PUT, DELETE…
Transfer Data: JSON or XML/HTML
Lightweight Heavyweight

Sender Data Standards Receiver

Communication

Sender Data Receiver


REST API COMMUNICATION MODEL

Operations Transfer Data


GET: (url) => response Dữ liệu trao đổi giữa Client và
POST: (url, data) => response REST API là JSON/XML
PUT: (url, data) => response
DELETE: (url) => response (null)
FIREBASE REALTIME DATABSE

RESTful API base URL

Data structure
FIREBASE REST API URLS

 EntryPoint URLs
https://poly-java-6-d4e0b-default-rtdb.firebaseio.com/
…users.json => all users
…users/PS09013.json => one user GET POST
…users/PS09013/name.json => name
…users/PS09013/contact.json => contact PUT DELETE
…users/PS09013/contact/email.json => email
CONSUME FIREBASE REST API
EntryPoint URL = “https://.../users/PS09013.json”
Data Structure = {
“id”: “?”, Firebase
“name”: “?”, REST API
“contact”: {
“email”: “?”,
“phone”: “?”

response
}

request
}
 Operations
 GET: request (url) => response (user)
 POST: request (url, user) => response (key)
 PUT: request (url, user) => response (user)
 DELETE: request (url) => response (null) REST Consumer
REST API EXCHANGE MODEL

Request: Method, URL [, JsonData]


Postman
Response: [JsonData]

Request:
Method: GET, POST, PUT, DELETE
URL: EntryPoint
JsonData: JSON
Response:
JsonData: JSON
(Web Browser) (Web Server)
AngularJS RestTemplate

Web Browser

Desktop App
URL, Jackson
ANGULARJS $HTTP SERVICE API

• get().then().catch() • post().then().catch()

GET POST

DELETE PUT

• delete().then().catch() • put().then().catch()
ANGULARJS $HTTP SERVICE
REST API Operations
$http.get(url).then(response => {}).catch(error => {})
$http.post(url, data).then(response => {}).catch(error => {}))
$http.put(url, data).then(response => {}).catch(error => {}))
$http.delete(url).then(response => {}).catch(error => {}))
Ví dụ
$http.get(“…/users.json”).then(response => {
var users = response.data;
})
.catch(error => {
console.log(“Lỗi”, error);
})
ANGULARJS - DEMO APPLICATION MODEL

controller

AngularJS
_form.html

$http
_table.html
JACKSON API

ObjectMapper JsonNode
readValue() get() JsonNode
readTree() findValue()
writeValue() asType()
writeValueAsString() iterator()
ObjectNode
createObjectNode() ObjectNode
put()
putObject()
putArray()
JAVA DESKTOP APPLICATION – DEMO APPLICATION MODEL

URL, Jackson
Swing JFrame

Event Handler
RESTTEMPLATE API

• getForObject() • postForObject()

GET POST

DELETE PUT

• delete() • put()
RESTTEMPLATE API

RestTemplate: Thực hiện các REST Operations


getForObject(url, responseType<T>): T
delete(url)
postForObject(url, httpEntity, responseType<T>): T
put(url, httpEntity)
HttpEntity<T>: Đóng gói dữ liệu json gửi đến REST API
new HttpEntity<>(T)
RESTTEMPLATE PROGRAMMING MODEL
ENTITY CLASSES

HashMap<String, User>

UserMap

User
id: String
name: String
contact: Contact

Contact
email: String
phone: String
SPRING RESTTEMPLATE – DEMO APPLICATION MODEL

RestTemplate
Controller
_form.html

_table.html

Web Browser
SUMMARY

Web Service
REST & REST API
Firebase REST API & Postman
Consume REST API with
AngularJS
Java.net.URL
RestTemplate

You might also like