QUICK RECIPE:
HOW TO END MANUAL REST-API
CLIENT CODING
@johannes_fiala
Calling APIs manually
$.getJSON(backend_url ???, data ???, function(response) {
response.???
}
Calling APIs
using a generated SDK
Ingredients
 Swagger API docs
 need to be supplied by the API provider
 can be provided automatically using e.g. Spring Boot +
Swagger-Springfox
 see Swagger.io –Tools & Integrations
 You can also do “API first“ using Swagger-Editor
 Swagger-Codegen
 generate client code in 20+ languages
 Static Javascript support requested in 2013
 … added during dotjs 2015
 … available since 2015-12-07 05:00
Sample REST model using Spring
Boot + Springfox
Swagger API example
Model definition
Steps to generate
Javascript client code
 Prepare
 Checkout Swagger-Codegen
 Generate
 Generate JS client stubs
 Code
 Enjoy code completion for the REST API in
Javascript & HTML
JS Mustache templates for
Swagger-Codegen
Generate client code using
Swagger-Codegen
 java -i http://localhost:8080/v2/api-docs
-l javascript -o c:/myproject
Generated Javascript Model:
Code completion for User
Code completion in HTML
Code completion
for all API operations
Current state of
Javascript support
 API support
 Jsdoc comments + type hints
 Model support
 Jsdoc comments + type hints
 Enumeration support
 Getter/Setter support
 Constructor for required properties
 Print using toJson()
 Plain JS + AMD usage supported
It‘s not yet perfect …
 Support forTypescript
 data types, inheritance, interfaces, enumerations
 More annotations / decorator support
 See Java BeanValidationAPI: @NotNull, @Size,
@Min, @Max, …
 ???
It‘s up to you …
Thank you for your
attention!
 Swagger-Codegen:
 https://github.com/swagger-api/swagger-codegen
(master / scheduled for release 2.1.5)
 Full demo for Spring Boot:
Devoxx 2015 @Youtube
 Questions?
Contact: @johannes_fiala
Resources
 Swagger
http://swagger.io/
 Swagger-Codegen
https://github.com/swagger-api/swagger-codegen
 Spring Boot
http://projects.spring.io/spring-boot/
 Swagger-Springfox
https://github.com/springfox/springfox
 Swagger-UI
https://github.com/swagger-api/swagger-ui

Rest api code completion for javascript - dotjs 2015