SlideShare une entreprise Scribd logo
1  sur  15
Télécharger pour lire hors ligne
Introduction to Retrofit
or how to say goodbye to
almost correct API doc
第十一回 #渋谷java
Kazuhiro Serizawa
About me
• Name: Kazuhiro Serizawa
• Job: Web engineer
• Like: RoR, AngularJS
• Github: serihiro
• Twitter: seri_k
• Blog: http://serihiro.hatenablog.com/
What is Retrofit?
• A type-safe REST client for Android and Java
• Created by Square, Inc.
• 5493 Starred (at 2015.5.25)
• Latest version is 1.9.0 (at 2015.5.25)
• Gtihub: https://github.com/square/retrofit
• Docs: http://square.github.io/retrofit/
How to use?
In the case you access to Todo API
GET /todo … response todo list with json
GET /todo/:id … response todo with json
POST /todo … create new todo resource
code sample
https://github.com/serihiro/retrofit-implement-samle
1. Define API Interface
public interface TodoService {
@GET("/todos")
List<Todo> list();
@GET("/todos/{id}")
Todo show(@Path("id") Integer id);
@POST("/todos")
void create(@Body Todo todo);
}
2. Implement model class
for API response
public class Todo {
public Integer id = null;
public String todo = "";
public boolean done = false;
public Todo(String todo, boolean done) {
this.todo = todo;
this.done = done;
}
}
3. Generate implementation
for API interface
RestAdapter restAdapter = new
RestAdapter.Builder()
.setEndpoint( http://localhost:3000")
.build();

TodoService todoService =
restAdapter.create(TodoService.class);
4.call api
List<Todo> todos = todoService.list();
Todo todo = todoService.show(1);
todoService.create(new Todo( drink beer , false));
Typesafe
• Define endpoints as method
• So rest client with Retrofit knows what
endpoints API has
Interface as API
Specification
• Client user can know what endpoint exists
• User get 404 with wrong endpoint name spell !!!
• No more almost correct API documents : )
public interface TodoService {

@GET("/todos")

List<Todo> list();

@GET("/todos/{id}")

Todo show(@Path("id") int id);

@POST("/todos")

void create(@Body Todo todo, Callback<Result> cb);

}
feature: ASYNC request
with callback
• Synchronous
• void create(@Body Todo todo)
• Asynchronous
• void create(@Body Todo todo,
Callback<Result> cb);
converters
Retrofit uses Gson to parse response body as a
default.

There are following converter class instead of Gson;
• Simple XML Parser(Uses simple framework)
• Jackson
• Google Protocol Buffer
• Wire
source: https://github.com/square/retrofit/tree/master/retrofit-converters
feature: static/dynamic
header value
Static Header

@Headers({

"Accept: application/vnd.github.v3.full+json ,

"User-Agent: Retrofit-Sample-App"

})

@GET("/todos")

List<Todo> list();
feature: static/dynamic
header value
Dynamic Header


@GET("/todos")

List<Todo> list(@Header("Authorization")
String authorization);
Conclusion
Retrofit provides
no more

almost correct 

api-document life :)

Contenu connexe

Tendances

Django rest framework tips and tricks
Django rest framework   tips and tricksDjango rest framework   tips and tricks
Django rest framework tips and tricksxordoquy
 
WuKong - Framework for Integrated Test
WuKong - Framework for Integrated TestWuKong - Framework for Integrated Test
WuKong - Framework for Integrated TestSummer Lu
 
Spring Bootを触ってみた
Spring Bootを触ってみたSpring Bootを触ってみた
Spring Bootを触ってみたonozaty
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + djangoNina Zakharenko
 
Spring Booted, But... @JCConf 16', Taiwan
Spring Booted, But... @JCConf 16', TaiwanSpring Booted, But... @JCConf 16', Taiwan
Spring Booted, But... @JCConf 16', TaiwanPei-Tang Huang
 
StirTrek 2018 - Rapid API Development with Sails
StirTrek 2018 - Rapid API Development with SailsStirTrek 2018 - Rapid API Development with Sails
StirTrek 2018 - Rapid API Development with SailsJustin James
 
Python Flask app deployed to OPenShift using Wercker CI
Python Flask app deployed to OPenShift using Wercker CIPython Flask app deployed to OPenShift using Wercker CI
Python Flask app deployed to OPenShift using Wercker CIBruno Rocha
 
Writing documentation with Asciidoctor
Writing documentation  with  AsciidoctorWriting documentation  with  Asciidoctor
Writing documentation with AsciidoctorJérémie Bresson
 
Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)Nordic APIs
 
Chrome Devtools Protocol via Selenium/Appium (English)
Chrome Devtools Protocol via Selenium/Appium (English)Chrome Devtools Protocol via Selenium/Appium (English)
Chrome Devtools Protocol via Selenium/Appium (English)Kazuaki Matsuo
 
API Design & Security in django
API Design & Security in djangoAPI Design & Security in django
API Design & Security in djangoTareque Hossain
 
Git Workshop
Git WorkshopGit Workshop
Git Workshopalexyork
 
Python from zero to hero (Twitter Explorer)
Python from zero to hero (Twitter Explorer)Python from zero to hero (Twitter Explorer)
Python from zero to hero (Twitter Explorer)Yuriy Senko
 
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...Atlassian
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Vikas Chauhan
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecturepostrational
 
PWA 與 Service Worker
PWA 與 Service WorkerPWA 與 Service Worker
PWA 與 Service WorkerAnna Su
 
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other ToolsCool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other ToolsRyan Weaver
 

Tendances (20)

Django rest framework tips and tricks
Django rest framework   tips and tricksDjango rest framework   tips and tricks
Django rest framework tips and tricks
 
WuKong - Framework for Integrated Test
WuKong - Framework for Integrated TestWuKong - Framework for Integrated Test
WuKong - Framework for Integrated Test
 
Spring Bootを触ってみた
Spring Bootを触ってみたSpring Bootを触ってみた
Spring Bootを触ってみた
 
Djangocon 2014 angular + django
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
 
Spring Booted, But... @JCConf 16', Taiwan
Spring Booted, But... @JCConf 16', TaiwanSpring Booted, But... @JCConf 16', Taiwan
Spring Booted, But... @JCConf 16', Taiwan
 
StirTrek 2018 - Rapid API Development with Sails
StirTrek 2018 - Rapid API Development with SailsStirTrek 2018 - Rapid API Development with Sails
StirTrek 2018 - Rapid API Development with Sails
 
Python Flask app deployed to OPenShift using Wercker CI
Python Flask app deployed to OPenShift using Wercker CIPython Flask app deployed to OPenShift using Wercker CI
Python Flask app deployed to OPenShift using Wercker CI
 
Writing documentation with Asciidoctor
Writing documentation  with  AsciidoctorWriting documentation  with  Asciidoctor
Writing documentation with Asciidoctor
 
Git training
Git trainingGit training
Git training
 
Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)Do you want a SDK with that API? (Nordic APIS April 2014)
Do you want a SDK with that API? (Nordic APIS April 2014)
 
Chrome Devtools Protocol via Selenium/Appium (English)
Chrome Devtools Protocol via Selenium/Appium (English)Chrome Devtools Protocol via Selenium/Appium (English)
Chrome Devtools Protocol via Selenium/Appium (English)
 
API Design & Security in django
API Design & Security in djangoAPI Design & Security in django
API Design & Security in django
 
Git Workshop
Git WorkshopGit Workshop
Git Workshop
 
Python from zero to hero (Twitter Explorer)
Python from zero to hero (Twitter Explorer)Python from zero to hero (Twitter Explorer)
Python from zero to hero (Twitter Explorer)
 
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
Satellite Apps around the Cloud: Integrating your infrastructure with JIRA St...
 
Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1Laravel Beginners Tutorial 1
Laravel Beginners Tutorial 1
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Workshop 15: Ionic framework
Workshop 15: Ionic frameworkWorkshop 15: Ionic framework
Workshop 15: Ionic framework
 
PWA 與 Service Worker
PWA 與 Service WorkerPWA 與 Service Worker
PWA 與 Service Worker
 
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other ToolsCool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
 

En vedette

UXデザインワークショップ資料 by ATOMOS DESIGN
UXデザインワークショップ資料 by ATOMOS DESIGNUXデザインワークショップ資料 by ATOMOS DESIGN
UXデザインワークショップ資料 by ATOMOS DESIGNAkihiko Kodama
 
AWS Lambda with Java/Scala #渋谷Java 第十二回
AWS Lambda with Java/Scala #渋谷Java 第十二回AWS Lambda with Java/Scala #渋谷Java 第十二回
AWS Lambda with Java/Scala #渋谷Java 第十二回hajime ni
 
SFA運用の秘訣と定着化のコツセミナー資料
SFA運用の秘訣と定着化のコツセミナー資料SFA運用の秘訣と定着化のコツセミナー資料
SFA運用の秘訣と定着化のコツセミナー資料NetyearGroup
 
Hello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight JavaHello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight Javay_taka_23
 
カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)
カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)
カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)さくらインターネット株式会社
 
Javaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだかJavaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだかHiroki Uchida
 
Adaptive Path's Guide To Experience Mapping (Japanese Edition)
Adaptive Path's Guide To Experience Mapping (Japanese Edition)Adaptive Path's Guide To Experience Mapping (Japanese Edition)
Adaptive Path's Guide To Experience Mapping (Japanese Edition)Kazumichi (Mario) Sakata
 
僕とメロス 第零章~第弐章
僕とメロス 第零章~第弐章僕とメロス 第零章~第弐章
僕とメロス 第零章~第弐章konta0223
 
リクルート式 自然言語処理技術の適応事例紹介
リクルート式 自然言語処理技術の適応事例紹介リクルート式 自然言語処理技術の適応事例紹介
リクルート式 自然言語処理技術の適応事例紹介Recruit Technologies
 

En vedette (9)

UXデザインワークショップ資料 by ATOMOS DESIGN
UXデザインワークショップ資料 by ATOMOS DESIGNUXデザインワークショップ資料 by ATOMOS DESIGN
UXデザインワークショップ資料 by ATOMOS DESIGN
 
AWS Lambda with Java/Scala #渋谷Java 第十二回
AWS Lambda with Java/Scala #渋谷Java 第十二回AWS Lambda with Java/Scala #渋谷Java 第十二回
AWS Lambda with Java/Scala #渋谷Java 第十二回
 
SFA運用の秘訣と定着化のコツセミナー資料
SFA運用の秘訣と定着化のコツセミナー資料SFA運用の秘訣と定着化のコツセミナー資料
SFA運用の秘訣と定着化のコツセミナー資料
 
Hello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight JavaHello, Type Systems! - Introduction to Featherweight Java
Hello, Type Systems! - Introduction to Featherweight Java
 
カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)
カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)
カスタマーサポートだからこそ提供できる価値とは?(CS Night -ネット企業のカスタマーサポート戦略を考えよう! -)
 
Javaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだかJavaの資格試験(OCJ-P)を取って何を学んだか
Javaの資格試験(OCJ-P)を取って何を学んだか
 
Adaptive Path's Guide To Experience Mapping (Japanese Edition)
Adaptive Path's Guide To Experience Mapping (Japanese Edition)Adaptive Path's Guide To Experience Mapping (Japanese Edition)
Adaptive Path's Guide To Experience Mapping (Japanese Edition)
 
僕とメロス 第零章~第弐章
僕とメロス 第零章~第弐章僕とメロス 第零章~第弐章
僕とメロス 第零章~第弐章
 
リクルート式 自然言語処理技術の適応事例紹介
リクルート式 自然言語処理技術の適応事例紹介リクルート式 自然言語処理技術の適応事例紹介
リクルート式 自然言語処理技術の適応事例紹介
 

Similaire à Introduction to Retrofit

Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Andres Almiray
 
Test-Driven Documentation for your REST(ful) service
Test-Driven Documentation for your REST(ful) serviceTest-Driven Documentation for your REST(ful) service
Test-Driven Documentation for your REST(ful) serviceJeroen Reijn
 
Extending Retrofit for fun and profit
Extending Retrofit for fun and profitExtending Retrofit for fun and profit
Extending Retrofit for fun and profitMatthew Clarke
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterSachin G Kulkarni
 
Building Better Web APIs with Rails
Building Better Web APIs with RailsBuilding Better Web APIs with Rails
Building Better Web APIs with RailsAll Things Open
 
iOS Swift application architecture
iOS Swift application architectureiOS Swift application architecture
iOS Swift application architectureRomain Rochegude
 
Build REST API clients for AngularJS
Build REST API clients for AngularJSBuild REST API clients for AngularJS
Build REST API clients for AngularJSAlmog Baku
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to MissJava Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to MissAndres Almiray
 
Tips and Tricks for Building Visual Studio Workflows
Tips and Tricks for Building Visual Studio WorkflowsTips and Tricks for Building Visual Studio Workflows
Tips and Tricks for Building Visual Studio WorkflowsMalin De Silva
 
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & MobileIVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & MobileAmazon Web Services Japan
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsTom Johnson
 
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...Amazon Web Services
 
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...Amazon Web Services
 
SharePoint 2013 REST APIs
SharePoint 2013 REST APIsSharePoint 2013 REST APIs
SharePoint 2013 REST APIsGiuseppe Marchi
 
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
Writing highly scalable WebSocket using the Atmosphere Framework and ScalaWriting highly scalable WebSocket using the Atmosphere Framework and Scala
Writing highly scalable WebSocket using the Atmosphere Framework and Scalajfarcand
 
SharePoint 2013 APIs
SharePoint 2013 APIsSharePoint 2013 APIs
SharePoint 2013 APIsJohn Calvert
 
Are you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint AppsAre you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint AppsLiam Cleary [MVP]
 
Developing on the aloashbei platform
Developing on the aloashbei platformDeveloping on the aloashbei platform
Developing on the aloashbei platformpycharmer
 

Similaire à Introduction to Retrofit (20)

Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss
 
Test-Driven Documentation for your REST(ful) service
Test-Driven Documentation for your REST(ful) serviceTest-Driven Documentation for your REST(ful) service
Test-Driven Documentation for your REST(ful) service
 
Extending Retrofit for fun and profit
Extending Retrofit for fun and profitExtending Retrofit for fun and profit
Extending Retrofit for fun and profit
 
REST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in CodeigniterREST API Best Practices & Implementing in Codeigniter
REST API Best Practices & Implementing in Codeigniter
 
Building Better Web APIs with Rails
Building Better Web APIs with RailsBuilding Better Web APIs with Rails
Building Better Web APIs with Rails
 
iOS Swift application architecture
iOS Swift application architectureiOS Swift application architecture
iOS Swift application architecture
 
Build REST API clients for AngularJS
Build REST API clients for AngularJSBuild REST API clients for AngularJS
Build REST API clients for AngularJS
 
Java Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to MissJava Libraries You Can’t Afford to Miss
Java Libraries You Can’t Afford to Miss
 
Tips and Tricks for Building Visual Studio Workflows
Tips and Tricks for Building Visual Studio WorkflowsTips and Tricks for Building Visual Studio Workflows
Tips and Tricks for Building Visual Studio Workflows
 
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & MobileIVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
 
API Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIsAPI Workshop: Deep dive into REST APIs
API Workshop: Deep dive into REST APIs
 
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
AWS July Webinar Series: Overview: Build and Manage your APIs with Amazon API...
 
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
AWS July Webinar Series - Overview Build and Manage your APs with amazon api ...
 
SharePoint 2013 REST APIs
SharePoint 2013 REST APIsSharePoint 2013 REST APIs
SharePoint 2013 REST APIs
 
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
Writing highly scalable WebSocket using the Atmosphere Framework and ScalaWriting highly scalable WebSocket using the Atmosphere Framework and Scala
Writing highly scalable WebSocket using the Atmosphere Framework and Scala
 
Crafting APIs
Crafting APIsCrafting APIs
Crafting APIs
 
SharePoint 2013 APIs
SharePoint 2013 APIsSharePoint 2013 APIs
SharePoint 2013 APIs
 
Are you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint AppsAre you getting Sleepy. REST in SharePoint Apps
Are you getting Sleepy. REST in SharePoint Apps
 
Developing on the aloashbei platform
Developing on the aloashbei platformDeveloping on the aloashbei platform
Developing on the aloashbei platform
 
Plack - LPW 2009
Plack - LPW 2009Plack - LPW 2009
Plack - LPW 2009
 

Plus de Kazuhiro Serizawa

Rubyはとても「人間的」
Rubyはとても「人間的」Rubyはとても「人間的」
Rubyはとても「人間的」Kazuhiro Serizawa
 
第八回 #渋谷Java 最近のjava PaaS事情
第八回 #渋谷Java 最近のjava PaaS事情第八回 #渋谷Java 最近のjava PaaS事情
第八回 #渋谷Java 最近のjava PaaS事情Kazuhiro Serizawa
 
第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと
第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと
第六回 #渋谷java Javaを書き始めて 1年半が経って思うことKazuhiro Serizawa
 
第四回 #渋谷java オープニング
第四回 #渋谷java オープニング第四回 #渋谷java オープニング
第四回 #渋谷java オープニングKazuhiro Serizawa
 
Juzu Frameworkを使ってみた @第四回 渋谷java
Juzu Frameworkを使ってみた @第四回 渋谷javaJuzu Frameworkを使ってみた @第四回 渋谷java
Juzu Frameworkを使ってみた @第四回 渋谷javaKazuhiro Serizawa
 
第3回渋谷javaオープニング
第3回渋谷javaオープニング第3回渋谷javaオープニング
第3回渋谷javaオープニングKazuhiro Serizawa
 
第二回渋谷Java Opening #渋谷Java
第二回渋谷Java Opening #渋谷Java第二回渋谷Java Opening #渋谷Java
第二回渋谷Java Opening #渋谷JavaKazuhiro Serizawa
 
じっくりコトコト煮込んだJavaスープ
じっくりコトコト煮込んだJavaスープじっくりコトコト煮込んだJavaスープ
じっくりコトコト煮込んだJavaスープKazuhiro Serizawa
 
PHPerがJava屋になるために乗り越えたこと
PHPerがJava屋になるために乗り越えたことPHPerがJava屋になるために乗り越えたこと
PHPerがJava屋になるために乗り越えたことKazuhiro Serizawa
 
第一回Shibuya.javaオープニング
第一回Shibuya.javaオープニング第一回Shibuya.javaオープニング
第一回Shibuya.javaオープニングKazuhiro Serizawa
 

Plus de Kazuhiro Serizawa (10)

Rubyはとても「人間的」
Rubyはとても「人間的」Rubyはとても「人間的」
Rubyはとても「人間的」
 
第八回 #渋谷Java 最近のjava PaaS事情
第八回 #渋谷Java 最近のjava PaaS事情第八回 #渋谷Java 最近のjava PaaS事情
第八回 #渋谷Java 最近のjava PaaS事情
 
第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと
第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと
第六回 #渋谷java Javaを書き始めて 1年半が経って思うこと
 
第四回 #渋谷java オープニング
第四回 #渋谷java オープニング第四回 #渋谷java オープニング
第四回 #渋谷java オープニング
 
Juzu Frameworkを使ってみた @第四回 渋谷java
Juzu Frameworkを使ってみた @第四回 渋谷javaJuzu Frameworkを使ってみた @第四回 渋谷java
Juzu Frameworkを使ってみた @第四回 渋谷java
 
第3回渋谷javaオープニング
第3回渋谷javaオープニング第3回渋谷javaオープニング
第3回渋谷javaオープニング
 
第二回渋谷Java Opening #渋谷Java
第二回渋谷Java Opening #渋谷Java第二回渋谷Java Opening #渋谷Java
第二回渋谷Java Opening #渋谷Java
 
じっくりコトコト煮込んだJavaスープ
じっくりコトコト煮込んだJavaスープじっくりコトコト煮込んだJavaスープ
じっくりコトコト煮込んだJavaスープ
 
PHPerがJava屋になるために乗り越えたこと
PHPerがJava屋になるために乗り越えたことPHPerがJava屋になるために乗り越えたこと
PHPerがJava屋になるために乗り越えたこと
 
第一回Shibuya.javaオープニング
第一回Shibuya.javaオープニング第一回Shibuya.javaオープニング
第一回Shibuya.javaオープニング
 

Dernier

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Dernier (20)

Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

Introduction to Retrofit

  • 1. Introduction to Retrofit or how to say goodbye to almost correct API doc 第十一回 #渋谷java Kazuhiro Serizawa
  • 2. About me • Name: Kazuhiro Serizawa • Job: Web engineer • Like: RoR, AngularJS • Github: serihiro • Twitter: seri_k • Blog: http://serihiro.hatenablog.com/
  • 3. What is Retrofit? • A type-safe REST client for Android and Java • Created by Square, Inc. • 5493 Starred (at 2015.5.25) • Latest version is 1.9.0 (at 2015.5.25) • Gtihub: https://github.com/square/retrofit • Docs: http://square.github.io/retrofit/
  • 4. How to use? In the case you access to Todo API GET /todo … response todo list with json GET /todo/:id … response todo with json POST /todo … create new todo resource code sample https://github.com/serihiro/retrofit-implement-samle
  • 5. 1. Define API Interface public interface TodoService { @GET("/todos") List<Todo> list(); @GET("/todos/{id}") Todo show(@Path("id") Integer id); @POST("/todos") void create(@Body Todo todo); }
  • 6. 2. Implement model class for API response public class Todo { public Integer id = null; public String todo = ""; public boolean done = false; public Todo(String todo, boolean done) { this.todo = todo; this.done = done; } }
  • 7. 3. Generate implementation for API interface RestAdapter restAdapter = new RestAdapter.Builder() .setEndpoint( http://localhost:3000") .build();
 TodoService todoService = restAdapter.create(TodoService.class);
  • 8. 4.call api List<Todo> todos = todoService.list(); Todo todo = todoService.show(1); todoService.create(new Todo( drink beer , false));
  • 9. Typesafe • Define endpoints as method • So rest client with Retrofit knows what endpoints API has
  • 10. Interface as API Specification • Client user can know what endpoint exists • User get 404 with wrong endpoint name spell !!! • No more almost correct API documents : ) public interface TodoService {
 @GET("/todos")
 List<Todo> list();
 @GET("/todos/{id}")
 Todo show(@Path("id") int id);
 @POST("/todos")
 void create(@Body Todo todo, Callback<Result> cb);
 }
  • 11. feature: ASYNC request with callback • Synchronous • void create(@Body Todo todo) • Asynchronous • void create(@Body Todo todo, Callback<Result> cb);
  • 12. converters Retrofit uses Gson to parse response body as a default.
 There are following converter class instead of Gson; • Simple XML Parser(Uses simple framework) • Jackson • Google Protocol Buffer • Wire source: https://github.com/square/retrofit/tree/master/retrofit-converters
  • 13. feature: static/dynamic header value Static Header
 @Headers({
 "Accept: application/vnd.github.v3.full+json ,
 "User-Agent: Retrofit-Sample-App"
 })
 @GET("/todos")
 List<Todo> list();
  • 14. feature: static/dynamic header value Dynamic Header 
 @GET("/todos")
 List<Todo> list(@Header("Authorization") String authorization);
  • 15. Conclusion Retrofit provides no more
 almost correct 
 api-document life :)