Upload
vuqar-suleymanov
View
161
Download
1
Embed Size (px)
Citation preview
Vüqar Süleymanov
Java Developer at Cybernet
RESTful
• What is Web Services?
• About RESTful
• RESTful with Groovy & Grails
Web Service is online API which we call from code.
Web Service
What is Web Sevices?
Difference between java local api and online api.
Client App
Library
Library
Library
Web Services
Web Service
Web Service
Client
Post to your wallApps
Games
http://www.twitter.com http://api.twitter.com
HTML XML / JSON
Web Service Types
REST Web Services
SOAP Web Services
Client Server
HTTP request
HTTP response
HTTP exchange
Client Web Service
Data<html…>
…</html>
HTTP exchange
Protocol
Client Web Service
Message format
SOAPSimple Object Access Protocol
Protocol
ClientSOAP
Web Service
SOAP protocol
ClientREST
Web Service
XMLJSONText…
What is RESTful Web Sevices?
• RESTful services was first introduced in the year 2000 by Roy Fielding at the university of California. But RESTful was popular only the last few years.
• Many developers found SOAP hard to use. For example, working with SOAP in JavaScript means writing a ton of code to perform extremely simple tasks because you must create the required XML structure absolutely every time.
• REST provides a lighter weight alternative. Instead of using XML to make a request, REST relies on a simple URL in many cases.
• Resource based URIs
• HTTP methods
• HTTP status codes
• Message headers
What we need?
RESTful with Groovy on Grails
Different approaches
• Using just @Resource.
• With uri attribute.
• With explicit UrlMappings
• Using Controller
package com.grailsbaki
import grails.rest.Resource
@Resourceclass Job {
String name
static constraints = {
}}
package com.grailsbaki
import grails.rest.Resource
@Resource(uri=‘/category’)class Category {
String name
static hasMany = [jobs: Job]
static constraints = { }}
<?xml version="1.0" encoding="UTF-8"?><list> <category id="1"> <jobs /> <name>Information Technology</name> </category> <category id="2"> <jobs /> <name>Sales & Marketing</name> </category> <category id="3"> <jobs /> <name>Banking & Finance</name> </category></list>
http://localhost:8080/RESTfulOnGrails/category
<?xml version="1.0" encoding="UTF-8"?><list> <category id="1"> <jobs /> <name>Information Technology</name> </category> <category id="2"> <jobs /> <name>Sales & Marketing</name> </category> <category id="3"> <jobs /> <name>Banking & Finance</name> </category></list>
http://localhost:8080/RESTfulOnGrails/categories
package com.grailsbaki
import grails.rest.Resource
@Resource(uri=‘/category’, formats=[‘json’], readOnly = true)class Category {
String name
static hasMany = [jobs: Job]
static constraints = { }} [
{ "class": "com.grailsbaku.Category", "id": 1, "jobs": [], "name": "Information Technology" }, { "class": "com.grailsbaku.Category", "id": 2, "jobs": [], "name": "Sales & Marketing" }, { "class": "com.grailsbaku.Category", "id": 3, "jobs": [], "name": "Banking & Finance" }]
grails> url-mappings-report
"/categories" (resources: "category"){ "/jobs" (resources: "job")}
UrlMapping
http://localhost:8080/RESTfulOnGrails/categories/1/jobs<?xml version="1.0" encoding="UTF-8"?><list> <job id="1"> <name>Java Developer</name> </job> <job id="2"> <name>C# Developer</name> </job> <job id="3"> <name>Php Developer</name> </job></list>
Using Controller
def save(params) {new Person(name: params.name, surname: params.surname).save(flush: true)
}
def update() {print("update method")
}
def delete() {print("delete method")
}
def list() {def people = Person.list()respond people, [formats:['xml','json']]
request.withFormat { json { render people as JSON }
}}
static allowedMethods = [save:'POST',delete:['POST', 'DELETE'], update:'POST', list:'GET']
POSTMAN
The End