Top Banner
25

RESTful web services with Groovy on Grails by Vugar Suleymanov

Jul 28, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RESTful web services with Groovy on Grails by Vugar Suleymanov
Page 2: RESTful web services with Groovy on Grails by Vugar Suleymanov

Vüqar Süleymanov

Java Developer at Cybernet

Page 3: RESTful web services with Groovy on Grails by Vugar Suleymanov

RESTful

• What is Web Services?

• About RESTful

• RESTful with Groovy & Grails

Page 4: RESTful web services with Groovy on Grails by Vugar Suleymanov

Web Service is online API which we call from code.

Web Service

What is Web Sevices?

Page 5: RESTful web services with Groovy on Grails by Vugar Suleymanov

Difference between java local api and online api.

Client App

Library

Library

Library

Page 6: RESTful web services with Groovy on Grails by Vugar Suleymanov

Web Services

Web Service

Web Service

Client

Page 7: RESTful web services with Groovy on Grails by Vugar Suleymanov

Post to your wallApps

Games

Page 8: RESTful web services with Groovy on Grails by Vugar Suleymanov

http://www.twitter.com http://api.twitter.com

HTML XML / JSON

Page 9: RESTful web services with Groovy on Grails by Vugar Suleymanov

Web Service Types

REST Web Services

SOAP Web Services

Page 10: RESTful web services with Groovy on Grails by Vugar Suleymanov

Client Server

HTTP request

HTTP response

HTTP exchange

Page 11: RESTful web services with Groovy on Grails by Vugar Suleymanov

Client Web Service

Data<html…>

…</html>

HTTP exchange

Page 12: RESTful web services with Groovy on Grails by Vugar Suleymanov

Protocol

Client Web Service

Message format

SOAPSimple Object Access Protocol

Page 13: RESTful web services with Groovy on Grails by Vugar Suleymanov

Protocol

ClientSOAP

Web Service

SOAP protocol

ClientREST

Web Service

XMLJSONText…

Page 14: RESTful web services with Groovy on Grails by Vugar Suleymanov

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.

Page 15: RESTful web services with Groovy on Grails by Vugar Suleymanov

• Resource based URIs

• HTTP methods

• HTTP status codes

• Message headers

What we need?

Page 16: RESTful web services with Groovy on Grails by Vugar Suleymanov

RESTful with Groovy on Grails

Different approaches

• Using just @Resource.

• With uri attribute.

• With explicit UrlMappings

• Using Controller

Page 17: RESTful web services with Groovy on Grails by Vugar Suleymanov

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 = { }}

Page 18: RESTful web services with Groovy on Grails by Vugar Suleymanov

<?xml version="1.0" encoding="UTF-8"?><list> <category id="1"> <jobs /> <name>Information Technology</name> </category> <category id="2"> <jobs /> <name>Sales &amp; Marketing</name> </category> <category id="3"> <jobs /> <name>Banking &amp; Finance</name> </category></list>

http://localhost:8080/RESTfulOnGrails/category

Page 19: RESTful web services with Groovy on Grails by Vugar Suleymanov

<?xml version="1.0" encoding="UTF-8"?><list> <category id="1"> <jobs /> <name>Information Technology</name> </category> <category id="2"> <jobs /> <name>Sales &amp; Marketing</name> </category> <category id="3"> <jobs /> <name>Banking &amp; Finance</name> </category></list>

http://localhost:8080/RESTfulOnGrails/categories

Page 20: RESTful web services with Groovy on Grails by Vugar Suleymanov

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" }]

Page 21: RESTful web services with Groovy on Grails by Vugar Suleymanov

grails> url-mappings-report

Page 22: RESTful web services with Groovy on Grails by Vugar Suleymanov

"/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>

Page 23: RESTful web services with Groovy on Grails by Vugar Suleymanov

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']

Page 24: RESTful web services with Groovy on Grails by Vugar Suleymanov

POSTMAN

Page 25: RESTful web services with Groovy on Grails by Vugar Suleymanov

The End