添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
增加配置类:SwaggerConfig.java
package com.me.config;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author xufeng
* @version 1.0
* @date 2020/4/8 16:20
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
//api接口包扫描路径
private static final String SWAGGER_SCAN_BASE_PACKAGE = "com.me.controller";
private static final String VERSION = "1.0.0";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("问卷设计分析服务API") //设置文档的标题
.description("问卷设计分析服务 API 接口文档") // 设置文档的描述
.version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
.termsOfServiceUrl("http://www.baidu.com") // 设置文档的License信息->1.3 License information
.build();
}
}
@Configuration标记让spring加载该配置类,@EnableSwagger2启用swagger
最后在api接口的controller类上加各种注解就可以生成api文档了
类似于
@Api(description = "登录API接口")
@RestController
@Slf4j
public class LoginController {
@Autowired
private UserService userService;
@Autowired
private RedisUtil redisUtil;
@ApiOperation(value = "登录", notes = "登录接口信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "email", value = "邮箱", required = true, dataType = "String")
})
@RequestMapping("/login")
@ResponseBody
public HashMap forLogin(...){
......
}
}
然后启动项目,在项目端口后面加/swagger-ui.html就可以看到在线的接口文档了。