1. Import two dependencies
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger第三方ui依赖 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
Swaggeui recommends using third-party dependencies
2. Configure swagger using the java configuration class in the project
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* swagger公帮助我们生成按口文档
* 1:配置生成的文档信息
* 2:配置生成规则
*/
/**
* Docket封装接口文档信息
*
*/
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2) //指定文档风格
.apiInfo(apiInfo())
.select() //选择生成策略
.apis(RequestHandlerSelectors.basePackage("com.zstudyj.controller")) //选择要生成接口文档的类
.paths(PathSelectors.any())
.build();
}
/**
* 指定生成的文档中的封面信息:文档标题、 版本、作者等
* @return ApiInfo
*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("xx商城接口文档") //添加标题
.description("xx商城接口文档") //添加描述
.contact(new Contact("zxj","http:localhost:8081/doc.html","[email protected]"))
.version("v 2.0.1")
.build();
}
}
Configuration completed, access interfacehttp://ip:port/doc.html
3. Swagger annotation description
Swagger provides a set of annotations that can provide detailed descriptions of each interface.
@Api
Class annotation, add this annotation to the controller class to explain the function of the controller class
.
@ApiOperation
Method annotation. Add this annotation to the method to explain the function of the method.
Effect
@ApiImplicitParams
and@ApiImplicitParam
parameter annotations, indicating the parameter type, name, whether it is required, default value, etc.
Effect
@ApiModel
And@ApiModelProperty
when the interface parameters and return values are of object type, add annotations to the entity class.