Document your rest api using swagger - Devoxx 2015

23
@johannes_fiala #Devoxx #Swagger Document your REST-API and generate client-code using Swagger and Spring Johannes Fiala FWD `

Transcript of Document your rest api using swagger - Devoxx 2015

Page 1: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Document your REST-API and generate client-code

using Swagger and SpringJohannes Fiala

FWD

`

Page 2: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

In the next few minutes you‘ll learn how to…• provide documentation for your REST-API• access your API with a browser using Swagger UI• generate client code for accessing your API• get a nice picture of your API services & models

Page 3: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Swagger• A language-agnostic interface to REST APIs • allows to discover and understand the capabilities of a

service• Swagger removes the guesswork in calling the service• Swagger renders the API docs in JSON syntax

“A standardized way to describe an API in JSON syntax”

Source: https://github.com/swagger-api/swagger-spec

Page 4: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Technology stack• Spring• Spring Boot for fast start• Also possible for Spring REST applications• Springfox• For providing the dynamic api-docs• Swagger UI• For accessing the api using a browser• Swagger Codegen• For generating client code stubs

Page 5: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Big PictureYour REST-API

Springfox/v2/api-docs/

Swagger-UI/swagger/index.html

Swagger-Codegen

Client-CodeJava, PHP, C#, Ruby,

nodejs, Objective-C, …

Page 6: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Springfox• Provide complete api-docs for every

@RESTController• Services• Supported Verbs (GET/POST/…)• Request parameters/body• Response codes + body• Many customization options (hide attributes,

custom data types, …)

Page 7: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Swagger UI• Javascript application for accessing your complete

REST API using a browser• Lists all services directly from the (dynamic) api-

docs• Always consistent with your API!

Page 8: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Swagger Codegen• Client code stub generator (commandline)• Generates client stubs (customizable!)• Supported languages:• Java, C#, Dart, Flash, Groovy, JaxRS, NodeJS,

Objective-C, Perl, PHP, Python, Ruby, Scala, ….

Page 9: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Why generate client code?• Ensure consistency of your client code with the

API!• Makes code completion possible!• For service methods & model classes• Allows developers to read description for your

operations and models in the IDE• You get compilation errors if the API breaks with

newer versions!

Page 10: Document your rest api using swagger - Devoxx 2015

Demo

@YourTwitterHandle#Devoxx #Swagger

Page 11: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Swagger Codegenerator• Code generation templates:• DefaultCodegenerator.generate():• Scans Model Properties first• Then compliles the Mustache templates• Language specifics:• io.swagger.codegen.languages.*• Mustache files

Page 12: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Wrapup• Springfox: provide api-docs• Completely dynamic & Always consistent• Swagger-UI: access api-docs using browser• make your api-docs easily acessible for

testers/developers/…• Swagger-Codegen: generate client stubs• Get code completion• Keep your clients in sync• Swagger.Ed: display your API graphically

Page 13: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Enable Swagger/Springfox

• Add dependencies• Add @EnableSwagger2 to Application• Run your spring application

Page 14: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Springfox annotations• Controller: • @Api• Operations:• @ApiOperation – describe your service• @ApiResponse – Define error codes• Model:• @ApiModel(„description“)• @ApiModelProperty: description + required-flag• @JsonIgnore

Page 15: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Swagger-UI

• Ship together with your REST-service• Makes accessing your services easy• Protect the Swagger-UI if applicable

Page 16: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Use Swagger Codegen• Call using the commandline• Integrate with your build environment (e.g. using

Ant)

Page 17: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Swagger.Ed• Chrome Plugin for visually displaying you API• Allows users to graphically explore through your

services & models• Nice for „Big picture“ of your API

Page 18: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Who is using Swagger?• Paypal• Microsoft• Amazon AWS API gateway:• http://swagger.io/getting-started-with-the-amazon-swag

ger-importer/• You can import Swagger definitions here• You can manage your services using Amazon AWS then

Page 19: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

More sessions about Swagger• Thursday BOF 21:00-22:00:

How to get code completion for existing REST APIs

using Swagger (e.g. Facebook Graph API)

• Come if you like to see how this can be done!• Demo with Facebook Graph API

Page 20: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Links & Resources• Swagger.io

http://swagger.io• Swagger.io integrations

http://swagger.io/open-source-integrations/

• Springfoxhttp://springfox.github.io/springfox/

Page 21: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Links & Resources• Swagger UI

https://github.com/swagger-api/swagger-ui• Swagger Codegen

https://github.com/swagger-api/swagger-codegen • Chrome Plugin Swagger.ed

https://github.com/chefArchitect/apispots-browser-swaggered

Page 22: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Questions ???

Page 23: Document your rest api using swagger - Devoxx 2015

@johannes_fiala#Devoxx #Swagger

Thank you for your attention!• Contact: • [email protected]