swagger是什么
swagger是一个规范和完整的框架,用来生成、描述、调用和可视化 RESTful 风格的 Web 服务的接口文档。
使用swagger
第一步创建springboot工程在pom文件中加入相关的依赖
<!--
1.使用swagger要求jdk1.8及以上,否则swagger2不能运行
2.springboot集成swagger需要引入两个包即以上两个
-->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
第二步编写swagger配置类
/**
* 声明配置类
*/
@Configuration
/**
*开启sagger2
*/
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
//是否开启swagger,false则不在浏览器开启
.enable(true)
//配置swagger信息
.apiInfo(new ApiInfoBuilder()
.title("swagger测试文档")
.description("描述")
.version("1.0").build())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any()).build();
}
}
第三步编写controller
@RestController
@Api(tags = "测试接口")
public class TestController {
@GetMapping
@ApiOperation("测试方法")
public String test() {
return "你好swagger";
}
}
第四步访问http://localhost:8080/swagger-ui.html
看到有返回则成功