The document provides an overview of Swagger, a tool for creating interactive API documentation, client SDK generation, and API discoverability. It outlines various tools and configurations associated with Swagger, including specifications for using Swagger UI and integrating Swagger within a Grails application. Additionally, the document details annotations and configurations required to enhance API documentation, including several examples of how to use these features effectively.
Introduction to Swagger, highlighting its interactivity for documentation, SDK generation, and discoverability with companies like PayPal and Microsoft.
Overview of tools like Swagger UI, Swagger Editor for creating specifications, and SDK Generators for client SDK and server-side code generation.
Introduction of a case study using PetStore to illustrate Swagger's application in RESTful API.
Steps for integrating Swagger, including BuildConfig settings and dependencies required for proper functionality.
Details on configuring Swagger through BeanConfig, setting up titles, descriptions, versioning, and generating documentation for REST.
Overview of various Swagger annotations including Controller and Action Annotations, their purposes, and configurations for APIs. Additional Swagger annotations for detailed API documentation, including responses, parameters, and model properties.
Agenda
Introduction
Tools
Example
Integration
Annotations
Examples
Question
1. Swagger isa simple yet powerful representation of your RESTful API.
1. With a Swagger-enabled API, you get interactive documentation,
client SDK generation and discoverability.
1. Swagger helps companies like Apigee, Getty Images, Intuit,
LivingSocial, McKesson, Microsoft, Morningstar, and PayPal build the
best possible services with RESTful APIs.
Introduction
3.
Tools
Swagger UI
Use aSwagger
specification to drive
your API
documentation
Swagger Editor
An editor for designing
Swagger specifications
from scratch, using a
simple YAML structure
SDK Generators
Turn an API spec
into client SDKs or
server-side code
with Swagger
Codegen.
For More Details
Integration
BuildConfig
Add dependency resolutionurl
Add dependencies
Config
Add URL for swagger
Add init parameter
Allow user agents
resource.groovy
Set initial parameter value
for well documentation of
swagger
6.
BuildConfig Setting
repositories {
mavenRepo"http://repo1.maven.org/maven2/"
mavenRepo "http://maven.restlet.org"
}
dependencies {
compile 'io.swagger:swagger-core:1.5.8'
compile 'io.swagger:swagger-jaxrs:1.5.8'
runtime 'ch.qos.logback:logback-
classic:1.0.6'
}
plugins{
compile ":jaxrs:0.11"
}
Note:- It will be more preferable to add jaxrs jar into lib. Adding plugin will not work properly.
Property of Swagger
BeanConfig
setTitle(String) title Sets the title of the application.
setDescription(String) description Sets the description of the application.
setTermsOfServiceUr
l(String)
termsOfServ
iceUrl
Sets the URL of the application’s Terms of
Service.
setContact(String) contact
Sets the contact information for the
application.
setLicense(String) license Sets the license of the application
setLicenseUrl(String)
licenseUrl Sets the license URL of the application.
setVersion(String) version Sets the version of the API.
setBasePath(String) basePath Sets the basePath for the API calls.
setApiReader(String) apiReader Sets an API Reader class for Swagger.
setFilterClass(Sting) filterClass
Sets a security filter for Swagger’s
documentation.
setResourcePackage(String)
resourcePac
kage
Sets which package(s) Swagger should
scan to pick up resources. If there’s
more than one package, it can be a list
of comma-separated packages
setScan(boolean) scan
When set to true, Swagger will build
the documentation
10.
Code to generatedocument of REST
String[] schemes = ["http"] as String[]
swaggerConfig.setSchemes(schemes)
swaggerConfig.setScan(true)
def swagger = swaggerConfig.getSwagger()
Json.mapper().writeValueAsString(swagger);
11.
Annotations
Controller Annotation
@Api:- Marksa class as a Swagger resource.
@Path:- Uri to expose the controller.
Action Annotation
@GET, @POST, @PUT, @DELETE
@Path:- uri to access action
@ApiOperation:- Details of working of action and http
method against it
@Produces:- Produce the response formatting.
@Consumes:- Formatting data it will accept
@PathParam, @QueryParam, @HeaderParam, @FormParam
@BeanParam
12.
More annotation
@ApiResponses:- Awrapper to allow a list of multiple ApiResponse objects.
@ApiResponse:- Describes a possible response of an operation.
@ApiParam:- Adds additional meta-data for operation parameters.
@ApiImplicitParams:- A wrapper to allow a list of multiple ApiImplicitParam objects.
@ApiImplicitParam:- Represents a single parameter in an API Operation.
@ApiModel:- Provides additional information about Swagger models.
@ApiModelProperty:- Adds and manipulates data of a model property.
@Authorization:- Declares an authorization scheme to be used on a resource or an operation.
@AuthorizationScope:- Describes an OAuth2 authorization scope.
13.
Details of Annotations
@Api(
value=”uri”
description=”shortnotes”
consume=”format of data it will accept”
)
@ApiOperation(
value=”details”,
notes=”more details”
response=Class
nickName=”nickname”
)
@ApiResponse(
code=int
message=”detail of output”
)
@ApiParam(
value=”details”
required=boolean
)
@ApiImplicitParam(
name=”name of parameter”
value=”details”
required=boolean
dataType=”param datatype”
paramType=”param type”
)