一、pom文件增加依赖
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
二、编写Swagger2配置类
@EnableSwagger2 //注解启用Swagger2
@Configuration
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径(controller层的路径)
.apis(RequestHandlerSelectors.basePackage("com.aop.demo.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("SpringBoot使用Swagger2构建API")
//创建人
.contact(new Contact("Ricky", "http://www.hao123.com", "1964924878@qq.com"))
//版本号
.version("1.0")
//描述
.description("sawgger 描述")
.build();
}
}
三、编写controller测试类
@RestController
public class AppController {
public static volatile Integer num = 0;
public static ReentrantLock lock = new ReentrantLock();
public static volatile LinkedBlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue();
private RedisAutoConfiguration redisAutoConfiguration;
private RedisUtil redisUtil;
@Autowired
private HelloSender sender;
public AppController(RedisAutoConfiguration redisAutoConfiguration, RedisUtil redisUtil) {
this.redisAutoConfiguration = redisAutoConfiguration;
this.redisUtil = redisUtil;
}
@GetMapping("/all")
public String getTest() throws Exception {
String msg = "hello";
return "success";
}
@GetMapping("/receive")
public String receive() throws Exception {
String msg = "hello";
return "success";
}
}
最后通过自己本地配置的端口号加上swagger
http://localhost:90/swagger-ui.html 就可以访问