1.Swagger Profile: previous era separation of back-end, back-end call either the front or the back-end calls to the backend, expect to have a good interface documentation, Swagger emergence of providing a standardized, solves this problem.
2. In Springboot in Swagger:
(1) pom.xml dependency added:
1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId>springfox-swagger2</artifactId> 4 <version>2.7.0</version> 5 </dependency> 6 <dependency> 7 <groupId>io.springfox</groupId> 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version>2.7.0</version> 10 </dependency>
(2) establish a swagger configuration files such as named swaggerconfig.java
1 package com.example.demo.config01; 2 3 4 import io.swagger.annotations.ApiOperation; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import springfox.documentation.builders.PathSelectors; 8 import springfox.documentation.builders.RequestHandlerSelectors; 9 import springfox.documentation.service.ApiInfo; 10 import springfox.documentation.service.Contact; 11 import springfox.documentation.spi.DocumentationType; 12 import springfox.documentation.spring.web.plugins.Docket; 13 import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 15 @Configuration 16 @EnableSwagger2 17 public class SwaggerConfig { 18 19 @Bean 20 public Docket applicationApi() { 21 return new Docket(DocumentationType.SWAGGER_2) 22 .groupName("fak") 23 .select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 24 .paths(PathSelectors.any()).build() 25 .apiInfo(application()); 26 } 27 28 private ApiInfo application() { 29 ApiInfo apiInfo = new ApiInfo("接口管理" 30 , "api接口说明" 31 , "0.1" 32 , "" 33 , new Contact("zwill", "", "") 34 ,"Connection display text" 35 , ""); 36 return apiInfo; 37 } 38 }
(3) test whether the configuration
1 package com.example.demo.controller; 2 3 import io.swagger.annotations.Api; 4 import io.swagger.annotations.ApiOperation; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @Api(tags = "Server") 9 @RestController 10 public class ServerController { 11 @ApiOperation(value = "提供一个简单的对外接口") 12 @GetMapping("/getServer") 13 public String ServerApi() { 14 return "hello"; 15 } 16 }
(4) The results show (the default port 8080 I visit this site to HTTP: // localhost: 8080 / Swagger-ui.html )
Click Try it out! Can be tested, it is not particularly convenient?
(5) Some common annotation swagger2
@Api ()
for the class; swagger represent this class is identified resource
tags- explanatory
value- are described, alternate tags may be used
, however, if there are multiple tags value, generates a plurality of list
@ApiOperation () used in the process; http request operation represents a
value used to describe a method
notes for prompting content
tags can regroup (Conditional use)
@ApiParam () used in the methods, parameters, field descriptions; represents a parameter add metadata (description, or if required, etc.)
name- parameter name
value- parameter Description
required- required
@ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收
value–表示对象名
description–描述
都可省略
@ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏
@ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上
比较简单, 这里不做举例
@ApiImplicitParam() 用于方法
表示单独的请求参数
@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
name–参数ming
value–参数说明
dataType–数据类型
paramType–参数类型
example–举例说明