SlideShare a Scribd company logo
1 of 21
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

What's hot

What's new in Visual Studio 2013 & TFS 2013
What's new in Visual Studio 2013 & TFS 2013What's new in Visual Studio 2013 & TFS 2013
What's new in Visual Studio 2013 & TFS 2013Danijel Malik
 
Enhance existing REST APIs (e.g. Facebook Graph API) with code completion us...
Enhance existing REST APIs  (e.g. Facebook Graph API) with code completion us...Enhance existing REST APIs  (e.g. Facebook Graph API) with code completion us...
Enhance existing REST APIs (e.g. Facebook Graph API) with code completion us...johannes_fiala
 
Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015johannes_fiala
 
Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014JWORKS powered by Ordina
 
Writer APIs in Java faster with Swagger Inflector
Writer APIs in Java faster with Swagger InflectorWriter APIs in Java faster with Swagger Inflector
Writer APIs in Java faster with Swagger InflectorTony Tam
 
O365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
O365Con18 - SharePoint Framework for Administrators - Waldek MastykarzO365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
O365Con18 - SharePoint Framework for Administrators - Waldek MastykarzNCCOMMS
 
Intro To React Native
Intro To React NativeIntro To React Native
Intro To React NativeFITC
 
Patterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 appsPatterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 appsPhil Leggetter
 
Swagger 2.0: Latest and Greatest
Swagger 2.0: Latest and GreatestSwagger 2.0: Latest and Greatest
Swagger 2.0: Latest and GreatestLaunchAny
 
Modern javascript
Modern javascriptModern javascript
Modern javascriptKevin Ball
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuštInfinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuštInfinum
 
Iterative Development with Swagger on the JDK
Iterative Development with Swagger on the JDKIterative Development with Swagger on the JDK
Iterative Development with Swagger on the JDKSwagger API
 
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...Sébastien Levert
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewShahed Chowdhuri
 
ASP.NET 5 Overview: Post RTM
ASP.NET 5 Overview: Post RTMASP.NET 5 Overview: Post RTM
ASP.NET 5 Overview: Post RTMShahed Chowdhuri
 
Branching Strategies: Feature Branches vs Branch by Abstraction
Branching Strategies: Feature Branches vs Branch by AbstractionBranching Strategies: Feature Branches vs Branch by Abstraction
Branching Strategies: Feature Branches vs Branch by AbstractionChris Birchall
 
Gwt overview & getting started
Gwt overview & getting startedGwt overview & getting started
Gwt overview & getting startedBinh Bui
 
Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0joescars
 

What's hot (20)

What's new in Visual Studio 2013 & TFS 2013
What's new in Visual Studio 2013 & TFS 2013What's new in Visual Studio 2013 & TFS 2013
What's new in Visual Studio 2013 & TFS 2013
 
Enhance existing REST APIs (e.g. Facebook Graph API) with code completion us...
Enhance existing REST APIs  (e.g. Facebook Graph API) with code completion us...Enhance existing REST APIs  (e.g. Facebook Graph API) with code completion us...
Enhance existing REST APIs (e.g. Facebook Graph API) with code completion us...
 
Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015Document your rest api using swagger - Devoxx 2015
Document your rest api using swagger - Devoxx 2015
 
Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014Documenting your REST API with Swagger - JOIN 2014
Documenting your REST API with Swagger - JOIN 2014
 
Writer APIs in Java faster with Swagger Inflector
Writer APIs in Java faster with Swagger InflectorWriter APIs in Java faster with Swagger Inflector
Writer APIs in Java faster with Swagger Inflector
 
O365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
O365Con18 - SharePoint Framework for Administrators - Waldek MastykarzO365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
O365Con18 - SharePoint Framework for Administrators - Waldek Mastykarz
 
Intro To React Native
Intro To React NativeIntro To React Native
Intro To React Native
 
Patterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 appsPatterns and practices for building enterprise-scale HTML5 apps
Patterns and practices for building enterprise-scale HTML5 apps
 
Swagger 2.0: Latest and Greatest
Swagger 2.0: Latest and GreatestSwagger 2.0: Latest and Greatest
Swagger 2.0: Latest and Greatest
 
Modern javascript
Modern javascriptModern javascript
Modern javascript
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuštInfinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
 
Iterative Development with Swagger on the JDK
Iterative Development with Swagger on the JDKIterative Development with Swagger on the JDK
Iterative Development with Swagger on the JDK
 
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
SharePoint Saturday Vancouver - SharePoint Framework, Angular and Azure Funct...
 
ASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with OverviewASP.NET Core MVC + Web API with Overview
ASP.NET Core MVC + Web API with Overview
 
ASP.NET Core Unit Testing
ASP.NET Core Unit TestingASP.NET Core Unit Testing
ASP.NET Core Unit Testing
 
ASP.NET 5 Overview: Post RTM
ASP.NET 5 Overview: Post RTMASP.NET 5 Overview: Post RTM
ASP.NET 5 Overview: Post RTM
 
Branching Strategies: Feature Branches vs Branch by Abstraction
Branching Strategies: Feature Branches vs Branch by AbstractionBranching Strategies: Feature Branches vs Branch by Abstraction
Branching Strategies: Feature Branches vs Branch by Abstraction
 
Gwt overview & getting started
Gwt overview & getting startedGwt overview & getting started
Gwt overview & getting started
 
ASP.NET Core 1.0 Overview
ASP.NET Core 1.0 OverviewASP.NET Core 1.0 Overview
ASP.NET Core 1.0 Overview
 
Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0Getting Started with ASP.net Core 1.0
Getting Started with ASP.net Core 1.0
 

Similar to Swagger - Making REST APIs friendlier

Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...IncQuery Labs
 
Comparison of-angular-8 vs react-js
Comparison of-angular-8 vs react-jsComparison of-angular-8 vs react-js
Comparison of-angular-8 vs react-jseasyjobworld
 
Angular2.0@Shanghai0319
Angular2.0@Shanghai0319Angular2.0@Shanghai0319
Angular2.0@Shanghai0319Bibby Chung
 
How to build a JavaScript toolkit
How to build a JavaScript toolkitHow to build a JavaScript toolkit
How to build a JavaScript toolkitMichael Nelson
 
Javascript Best Practices and Intro to Titanium
Javascript Best Practices and Intro to TitaniumJavascript Best Practices and Intro to Titanium
Javascript Best Practices and Intro to TitaniumTechday7
 
Mvvm knockout vs angular
Mvvm knockout vs angularMvvm knockout vs angular
Mvvm knockout vs angularBasarat Syed
 
Single Page Applications - Desert Code Camp 2012
Single Page Applications - Desert Code Camp 2012Single Page Applications - Desert Code Camp 2012
Single Page Applications - Desert Code Camp 2012Adam Mokan
 
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014Phil Leggetter
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?Balajihope
 
Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Ganesh Kondal
 
APIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidadAPIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidadSoftware Guru
 
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularEscaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularMark Leusink
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners WorkshopSathish VJ
 
Mastering react with redux
Mastering react with reduxMastering react with redux
Mastering react with reduxGaurav Singh
 

Similar to Swagger - Making REST APIs friendlier (20)

Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...Lessons learned from building Eclipse-based add-ons for commercial modeling t...
Lessons learned from building Eclipse-based add-ons for commercial modeling t...
 
Comparison of-angular-8 vs react-js
Comparison of-angular-8 vs react-jsComparison of-angular-8 vs react-js
Comparison of-angular-8 vs react-js
 
Angular2.0@Shanghai0319
Angular2.0@Shanghai0319Angular2.0@Shanghai0319
Angular2.0@Shanghai0319
 
How to build a JavaScript toolkit
How to build a JavaScript toolkitHow to build a JavaScript toolkit
How to build a JavaScript toolkit
 
Angular vs react
Angular vs reactAngular vs react
Angular vs react
 
Javascript Best Practices and Intro to Titanium
Javascript Best Practices and Intro to TitaniumJavascript Best Practices and Intro to Titanium
Javascript Best Practices and Intro to Titanium
 
Javascript frameworks
Javascript frameworksJavascript frameworks
Javascript frameworks
 
Protractor survival guide
Protractor survival guideProtractor survival guide
Protractor survival guide
 
Mvvm knockout vs angular
Mvvm knockout vs angularMvvm knockout vs angular
Mvvm knockout vs angular
 
Single Page Applications - Desert Code Camp 2012
Single Page Applications - Desert Code Camp 2012Single Page Applications - Desert Code Camp 2012
Single Page Applications - Desert Code Camp 2012
 
Angular js 1.3 basic tutorial
Angular js 1.3 basic tutorialAngular js 1.3 basic tutorial
Angular js 1.3 basic tutorial
 
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
Using BladeRunnerJS to Build Front-End Apps that Scale - Fluent 2014
 
What is Mean Stack Development ?
What is Mean Stack Development ?What is Mean Stack Development ?
What is Mean Stack Development ?
 
Top java script frameworks ppt
Top java script frameworks pptTop java script frameworks ppt
Top java script frameworks ppt
 
Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5Tech io spa_angularjs_20130814_v0.9.5
Tech io spa_angularjs_20130814_v0.9.5
 
APIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidadAPIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidad
 
SGCE 2015 REST APIs
SGCE 2015 REST APIsSGCE 2015 REST APIs
SGCE 2015 REST APIs
 
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularEscaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
 
AngularJS Beginners Workshop
AngularJS Beginners WorkshopAngularJS Beginners Workshop
AngularJS Beginners Workshop
 
Mastering react with redux
Mastering react with reduxMastering react with redux
Mastering react with redux
 

Recently uploaded

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 

Recently uploaded (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 

Swagger - Making REST APIs friendlier

  • 1. Miroslav Rešetar mresetar@croz.net @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

  1. 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.
  2. 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.