SlideShare a Scribd company logo
Miroslav Rešetar mresetar@croz.net
@MiroslavResetar
SWAGGER - MAKING
REST APIS FRIENDLIER
STORIES FROM THE TRENCHES
Swagger 101
• Message from the thin
• “Swagger™ is a specification and complete framework implementation for
describing, producing, consuming, and visualizing RESTful web services”
• Enables
• Server – to implement APIs
• Client – to connect to APIs
• UI – to visualize APIs
• Language agnostic
• Human and machine readable
• Mostly used for HTTP but supports others
• Speaks JSON and XML (REST format)
• Can be described using JSON or YAML file
2
De-facto REST API specification standard
3
End of API Spec Wars – Swagger won
4
OpenAPI - De jure specification
• In 2015 Swagger is donated to OpenAPI Initiative under Linux Foundation
• Members:
5
OpenAPI Specification v3.0.0
• Currently in use is Swagger 2.0
• That’s why recommended URL to retrieve Swagger spec. is /v2/api-docs
• New version v3.0.0 is currently RC and should arrive in few months
• Not backward compatible but lossless upgrade from 2.0 is possible
• Better support for JSON Schema
• Currently (oneOf, anyOf, not, nullable, deprecated, writeOnly is not supported)
• 3.0 supports
• links (HATEOS style)
• Callbacks (Webhook)
• Examples
6
Swagger 2 vs OpenAPI 3
7
How to start with Swagger?
• Get to know Swagger definition.
• I should read documentation? Seriously? Well no.
• Start using Swagger Editor
1. Import example (in old editor there is Open Example option)
2. Start changing things
3. When stuck go to documentation
4. Continue with step 2 until satisfied
8
What about JSON vs YAML? Which to choose?
• Favorite consultant answer: It DependsTM
• To be more concrete, use YAML if
• Dense format and readability is a priority
• You’re comfortable with reading and writing YAML
• Your tools support YAML
• Use JSON if
• Machine interoperability is a priority (JSON is supported basically everywhere)
• You are more comfortable with JSON
• Tools you will use have better support for JSON
• eg. json-schema-validator
9
Take the API first route
• Why not do the opposite? Code first and expose current implementation as
API?
• Has some benefits like
• Don’t need to know specification (but annotations)
• Tools generate documentation from code
• But:
• You’re in the wrong context! You are already coding not designing.
• Hard to include others (non-coders) in design phase
• API should be first class citizen and deserves proper attention like
• Separate project – API design project
• Is done before server mock or client implementation
• Should have approval from consumer and provide
• Consumer having greater weight in decision making
10
SpringFox – I bring annotation pollution
• 33 lines of code
• Only 9 is non-Swagger code
• 27% computing code is just
unacceptable
11
Swagger in Real Life – Example 1
• Problem: New development in banking sector, two teams needs to
communicate
• Swagger API first design approach
• Collaboration via editing one file
• Team members had to power to “ask for feature” by updating the specification
• Tools used:
• Swagger Editor
• Online version, start from Petstore sample and strip down unnecessary
• Specification format
• YAML – because greater readability & less lines to write
• Maven
• swagger-codegen-maven-plugin to generate API Java Model objects
12
Swagger in Real Life – Example 1
13
api-model
project
Swagger YAML API
swagger-codegen-maven-
plugin generate Java model
back-services
project
Nexus repository
Jenkins Maven
build
front-services
project
Ext.JS UI
API is build by Jenkins job
and artifact (JAR) is
published on Nexus
repository
Backends For Frontends
pattern
Single-purpose Edge
Services for UI
Proxies and adapts API for
UI app
API is implemented by
Spring Boot app by using
api-model as API model
spec.
Web application is end user
of the API
Swagger in Real Life – Example 2
• Problem: System integration with complex business domain problem (telco)
• Swagger API JSON schema first design approach
• Collaboration via editing one file editing sample JSON files
• Creating JSON Schemas from JSON examples
• Using those JSON schemas as models for Swagger API
• Tools used:
• Text editors & JSON 2 schema helpers (https://jsonschema.net/#/editor)
• Specification format
• JSON – because JSON schema supports better JSON than YAML 
• Gradle
• jsonschema2pojo-gradle-plugin – to generate API Java Model objects
• swagger-codegen-plugin – to generate REST clients stubs (actually just models
for know
14
Swagger in Real Life – Example 2
15
api-model
project
JSON Schema files
Generated API Java models
External system
Swagger-UI
exposes API and
schemas
Api-service and
web projects
Depends upon api-model
project
Implement business logic
such as message validation
upon JSON schema
Uses api-model project for
API client creation
Generators gotchas
• Plugins available don’t offer flexibility expected
• eg. swagger-codegen-plugin will put all model objects in the same package
• You may need to agree date-time format which you will use
• ISO 8601 is a start
• Eg. It is hard to have model to generate both LocaleDateTime and OffsetDateTime
(with timezone) or another place
• You may not want to regenerate classes every time
• eg. You need to edit them by hand
16
17
Swagger specification
Swagger-UI – one way to render spec.
Incorporating Swagger-UI into application
• (More than) few options:
1. You don’t include it
• You can use online available at http://petstore.swagger.io/ if your API is available
online
2. If you use SpringFox add SpringFox UI dependency
3. You can use webjars to add dependency to swagger-ui
4. Build Docker container with swagger-ui and web server
5. Clone swagger-ui git repo (dist) and copy it to web application
18
Swagger UI – webjars
• Add dependency
• Copy index.html from swagger-ui webjar
• Change url and other customization features
19
compile("org.webjars:swagger-ui:${orgWebjarsSwaggerUiVersion}")
compile("org.webjars:webjars-locator:${orgWebjarsWebjarsLocatorVersion}")
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "/v2/api-docs",
validatorUrl: null,
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})
window.ui = ui
}
Swagger UI gotchas
• If Swagger UI is not deployed together with application you need to
implement CORS support to serve API spec
• The base URL is defined by root level
• schemes
• host
• basePath
• Leave those empty if:
• Your endpoints use the same scheme, host and basePath as Swagger spec location
• This gets you functionality that “Try it out” works across environments
• Swagger-UI will try to download referenced JSON schemas
• You need to ensure that whole Swagger spec with referenced schemas can be
downloadable
20
Q & A
21

More Related Content

PPTX
Angular 2
Travis van der Font
 
PDF
Skinny Framework 1.0.0
Kazuhiro Sera
 
PPTX
Design p atterns
Amr Abd El Latief
 
PDF
How to generate a REST CXF3 application from Swagger ApacheConEU 2016
johannes_fiala
 
PDF
Conquering AngularJS Limitations
Valeri Karpov
 
PPT
Spring Boot. Boot up your development. JEEConf 2015
Strannik_2013
 
PPTX
Introduction to Angular 2
Tuan Trung Vo
 
PPTX
End to end test automation with cypress
Kushan Shalindra Amarasiri - Technical QE Specialist
 
Skinny Framework 1.0.0
Kazuhiro Sera
 
Design p atterns
Amr Abd El Latief
 
How to generate a REST CXF3 application from Swagger ApacheConEU 2016
johannes_fiala
 
Conquering AngularJS Limitations
Valeri Karpov
 
Spring Boot. Boot up your development. JEEConf 2015
Strannik_2013
 
Introduction to Angular 2
Tuan Trung Vo
 
End to end test automation with cypress
Kushan Shalindra Amarasiri - Technical QE Specialist
 

What's hot (20)

PPTX
What's new in Visual Studio 2013 & TFS 2013
Danijel Malik
 
PPTX
Enhance existing REST APIs (e.g. Facebook Graph API) with code completion us...
johannes_fiala
 
PPTX
Document your rest api using swagger - Devoxx 2015
johannes_fiala
 
PDF
Documenting your REST API with Swagger - JOIN 2014
JWORKS powered by Ordina
 
PPTX
Writer APIs in Java faster with Swagger Inflector
Tony Tam
 
PDF
O365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
NCCOMMS
 
PDF
Intro To React Native
FITC
 
PDF
Patterns and practices for building enterprise-scale HTML5 apps
Phil Leggetter
 
PDF
Swagger 2.0: Latest and Greatest
LaunchAny
 
PDF
Modern javascript
Kevin Ball
 
PDF
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum
 
PPTX
Iterative Development with Swagger on the JDK
Swagger API
 
PPTX
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
Sébastien Levert
 
PPTX
ASP.NET Core MVC + Web API with Overview
Shahed Chowdhuri
 
PPTX
ASP.NET Core Unit Testing
Shahed Chowdhuri
 
PPTX
ASP.NET 5 Overview: Post RTM
Shahed Chowdhuri
 
PDF
Branching Strategies: Feature Branches vs Branch by Abstraction
Chris Birchall
 
PPTX
Gwt overview & getting started
Binh Bui
 
PPTX
ASP.NET Core 1.0 Overview
Shahed Chowdhuri
 
PPTX
Getting Started with ASP.net Core 1.0
joescars
 
What's new in Visual Studio 2013 & TFS 2013
Danijel Malik
 
Enhance existing REST APIs (e.g. Facebook Graph API) with code completion us...
johannes_fiala
 
Document your rest api using swagger - Devoxx 2015
johannes_fiala
 
Documenting your REST API with Swagger - JOIN 2014
JWORKS powered by Ordina
 
Writer APIs in Java faster with Swagger Inflector
Tony Tam
 
O365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
NCCOMMS
 
Intro To React Native
FITC
 
Patterns and practices for building enterprise-scale HTML5 apps
Phil Leggetter
 
Swagger 2.0: Latest and Greatest
LaunchAny
 
Modern javascript
Kevin Ball
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum
 
Iterative Development with Swagger on the JDK
Swagger API
 
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
Sébastien Levert
 
ASP.NET Core MVC + Web API with Overview
Shahed Chowdhuri
 
ASP.NET Core Unit Testing
Shahed Chowdhuri
 
ASP.NET 5 Overview: Post RTM
Shahed Chowdhuri
 
Branching Strategies: Feature Branches vs Branch by Abstraction
Chris Birchall
 
Gwt overview & getting started
Binh Bui
 
ASP.NET Core 1.0 Overview
Shahed Chowdhuri
 
Getting Started with ASP.net Core 1.0
joescars
 
Ad

Similar to Swagger - Making REST APIs friendlier (20)

PDF
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
IncQuery Labs
 
PPTX
Comparison of-angular-8 vs react-js
easyjobworld
 
PPTX
Angular2.0@Shanghai0319
Bibby Chung
 
PPTX
How to build a JavaScript toolkit
Michael Nelson
 
PPTX
Angular vs react
Infinijith Technologies
 
PPTX
Javascript Best Practices and Intro to Titanium
Techday7
 
PPTX
Javascript frameworks
RajkumarJangid7
 
PPTX
Protractor survival guide
László Andrási
 
PPTX
Mvvm knockout vs angular
Basarat Syed
 
KEY
Single Page Applications - Desert Code Camp 2012
Adam Mokan
 
PPTX
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
PDF
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Phil Leggetter
 
PPTX
What is Mean Stack Development ?
Balajihope
 
PPT
Top java script frameworks ppt
Omkarsoft Bangalore
 
PPTX
Tech io spa_angularjs_20130814_v0.9.5
Ganesh Kondal
 
PDF
APIs distribuidos con alta escalabilidad
Software Guru
 
PDF
SGCE 2015 REST APIs
Domingo Suarez Torres
 
PDF
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Mark Leusink
 
PPTX
AngularJS Beginners Workshop
Sathish VJ
 
PDF
Mastering react with redux
Gaurav Singh
 
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
IncQuery Labs
 
Comparison of-angular-8 vs react-js
easyjobworld
 
Angular2.0@Shanghai0319
Bibby Chung
 
How to build a JavaScript toolkit
Michael Nelson
 
Angular vs react
Infinijith Technologies
 
Javascript Best Practices and Intro to Titanium
Techday7
 
Javascript frameworks
RajkumarJangid7
 
Protractor survival guide
László Andrási
 
Mvvm knockout vs angular
Basarat Syed
 
Single Page Applications - Desert Code Camp 2012
Adam Mokan
 
Angular js 1.3 basic tutorial
Al-Mutaz Bellah Salahat
 
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Phil Leggetter
 
What is Mean Stack Development ?
Balajihope
 
Top java script frameworks ppt
Omkarsoft Bangalore
 
Tech io spa_angularjs_20130814_v0.9.5
Ganesh Kondal
 
APIs distribuidos con alta escalabilidad
Software Guru
 
SGCE 2015 REST APIs
Domingo Suarez Torres
 
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Mark Leusink
 
AngularJS Beginners Workshop
Sathish VJ
 
Mastering react with redux
Gaurav Singh
 
Ad

Recently uploaded (20)

PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PDF
Immersive experiences: what Pharo users do!
ESUG
 
PDF
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PDF
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
PPTX
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Summary Of Odoo 18.1 to 18.4 : The Way For Odoo 19
CandidRoot Solutions Private Limited
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Presentation about variables and constant.pptx
kr2589474
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Immersive experiences: what Pharo users do!
ESUG
 
Using licensed Data Loss Prevention (DLP) as a strategic proactive data secur...
Q-Advise
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
Exploring AI Agents in Process Industries
amoreira6
 
lesson-2-rules-of-netiquette.pdf.bshhsjdj
jasmenrojas249
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Can You Build Dashboards Using Open Source Visualization Tool.pptx
Varsha Nayak
 
GALILEO CRS SYSTEM | GALILEO TRAVEL SOFTWARE
philipnathen82
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 

Swagger - Making REST APIs friendlier

  • 1. Miroslav Rešetar [email protected] @MiroslavResetar SWAGGER - MAKING REST APIS FRIENDLIER STORIES FROM THE TRENCHES
  • 2. Swagger 101 • Message from the thin • “Swagger™ is a specification and complete framework implementation for describing, producing, consuming, and visualizing RESTful web services” • Enables • Server – to implement APIs • Client – to connect to APIs • UI – to visualize APIs • Language agnostic • Human and machine readable • Mostly used for HTTP but supports others • Speaks JSON and XML (REST format) • Can be described using JSON or YAML file 2
  • 3. De-facto REST API specification standard 3
  • 4. End of API Spec Wars – Swagger won 4
  • 5. OpenAPI - De jure specification • In 2015 Swagger is donated to OpenAPI Initiative under Linux Foundation • Members: 5
  • 6. OpenAPI Specification v3.0.0 • Currently in use is Swagger 2.0 • That’s why recommended URL to retrieve Swagger spec. is /v2/api-docs • New version v3.0.0 is currently RC and should arrive in few months • Not backward compatible but lossless upgrade from 2.0 is possible • Better support for JSON Schema • Currently (oneOf, anyOf, not, nullable, deprecated, writeOnly is not supported) • 3.0 supports • links (HATEOS style) • Callbacks (Webhook) • Examples 6
  • 7. Swagger 2 vs OpenAPI 3 7
  • 8. How to start with Swagger? • Get to know Swagger definition. • I should read documentation? Seriously? Well no. • Start using Swagger Editor 1. Import example (in old editor there is Open Example option) 2. Start changing things 3. When stuck go to documentation 4. Continue with step 2 until satisfied 8
  • 9. What about JSON vs YAML? Which to choose? • Favorite consultant answer: It DependsTM • To be more concrete, use YAML if • Dense format and readability is a priority • You’re comfortable with reading and writing YAML • Your tools support YAML • Use JSON if • Machine interoperability is a priority (JSON is supported basically everywhere) • You are more comfortable with JSON • Tools you will use have better support for JSON • eg. json-schema-validator 9
  • 10. Take the API first route • Why not do the opposite? Code first and expose current implementation as API? • Has some benefits like • Don’t need to know specification (but annotations) • Tools generate documentation from code • But: • You’re in the wrong context! You are already coding not designing. • Hard to include others (non-coders) in design phase • API should be first class citizen and deserves proper attention like • Separate project – API design project • Is done before server mock or client implementation • Should have approval from consumer and provide • Consumer having greater weight in decision making 10
  • 11. SpringFox – I bring annotation pollution • 33 lines of code • Only 9 is non-Swagger code • 27% computing code is just unacceptable 11
  • 12. Swagger in Real Life – Example 1 • Problem: New development in banking sector, two teams needs to communicate • Swagger API first design approach • Collaboration via editing one file • Team members had to power to “ask for feature” by updating the specification • Tools used: • Swagger Editor • Online version, start from Petstore sample and strip down unnecessary • Specification format • YAML – because greater readability & less lines to write • Maven • swagger-codegen-maven-plugin to generate API Java Model objects 12
  • 13. Swagger in Real Life – Example 1 13 api-model project Swagger YAML API swagger-codegen-maven- plugin generate Java model back-services project Nexus repository Jenkins Maven build front-services project Ext.JS UI API is build by Jenkins job and artifact (JAR) is published on Nexus repository Backends For Frontends pattern Single-purpose Edge Services for UI Proxies and adapts API for UI app API is implemented by Spring Boot app by using api-model as API model spec. Web application is end user of the API
  • 14. Swagger in Real Life – Example 2 • Problem: System integration with complex business domain problem (telco) • Swagger API JSON schema first design approach • Collaboration via editing one file editing sample JSON files • Creating JSON Schemas from JSON examples • Using those JSON schemas as models for Swagger API • Tools used: • Text editors & JSON 2 schema helpers (https://jsonschema.net/#/editor) • Specification format • JSON – because JSON schema supports better JSON than YAML  • Gradle • jsonschema2pojo-gradle-plugin – to generate API Java Model objects • swagger-codegen-plugin – to generate REST clients stubs (actually just models for know 14
  • 15. Swagger in Real Life – Example 2 15 api-model project JSON Schema files Generated API Java models External system Swagger-UI exposes API and schemas Api-service and web projects Depends upon api-model project Implement business logic such as message validation upon JSON schema Uses api-model project for API client creation
  • 16. Generators gotchas • Plugins available don’t offer flexibility expected • eg. swagger-codegen-plugin will put all model objects in the same package • You may need to agree date-time format which you will use • ISO 8601 is a start • Eg. It is hard to have model to generate both LocaleDateTime and OffsetDateTime (with timezone) or another place • You may not want to regenerate classes every time • eg. You need to edit them by hand 16
  • 17. 17 Swagger specification Swagger-UI – one way to render spec.
  • 18. Incorporating Swagger-UI into application • (More than) few options: 1. You don’t include it • You can use online available at http://petstore.swagger.io/ if your API is available online 2. If you use SpringFox add SpringFox UI dependency 3. You can use webjars to add dependency to swagger-ui 4. Build Docker container with swagger-ui and web server 5. Clone swagger-ui git repo (dist) and copy it to web application 18
  • 19. Swagger UI – webjars • Add dependency • Copy index.html from swagger-ui webjar • Change url and other customization features 19 compile("org.webjars:swagger-ui:${orgWebjarsSwaggerUiVersion}") compile("org.webjars:webjars-locator:${orgWebjarsWebjarsLocatorVersion}") window.onload = function() { // Build a system const ui = SwaggerUIBundle({ url: "/v2/api-docs", validatorUrl: null, dom_id: '#swagger-ui', presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }) window.ui = ui }
  • 20. Swagger UI gotchas • If Swagger UI is not deployed together with application you need to implement CORS support to serve API spec • The base URL is defined by root level • schemes • host • basePath • Leave those empty if: • Your endpoints use the same scheme, host and basePath as Swagger spec location • This gets you functionality that “Try it out” works across environments • Swagger-UI will try to download referenced JSON schemas • You need to ensure that whole Swagger spec with referenced schemas can be downloadable 20

Editor's Notes

  • #4: Yesterday, MuleSoft, the creators of RAML, announced that they have joined the Open API Initiative. Created by SmartBear Software and based on the wildly popular Swagger Specification, the OpenAPI Initiative is a Linux Foundation project with over 20 members, including Adobe, IBM, Google, Microsoft, and Salesforce.
  • #5: Yesterday, MuleSoft, the creators of RAML, announced that they have joined the Open API Initiative. Created by SmartBear Software and based on the wildly popular Swagger Specification, the OpenAPI Initiative is a Linux Foundation project with over 20 members, including Adobe, IBM, Google, Microsoft, and Salesforce.