一、什么是Swagger
Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。简单来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端分离解决方案。
就是一个api文档,根据代码动态的展示出现存接口。以供前端开发人员查阅测试。减少因沟通不及时导致的一系列多余操作
二、引入Swagger2
1、导入依赖
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
2、配置swagger
- 改yml
#swagger配置
swagger:
enable: true
- 写配置类
package com.lm.config;
import org.springframework.beans.factory.annotation.Value;
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;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Value("${swagger.enable}")
private boolean enableSwagger;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启
.enable(enableSwagger)
.select()
//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage("com.lm.controller"))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题
.title("Spring-boot项目")
//文档描述
.description("V1.0接口规范")
//服务条款URL
.termsOfServiceUrl("http://127.0.0.1:6060/")
//版本号
.version("1.0.0")
.build();
}
}
三、使用swagger2
1、实体类 (不写也行)
@ApiModel(value = "用户类")
public class User {
@ApiModelProperty(value = "用户id")
private int id;
.............
}
2、控制层
@Api(value = "用户接口",tags = "用户")
public class BootController {
@Resource
private UserService userService;
@RequestMapping("/testJson")
@ApiOperation(value="测试",httpMethod="GET",notes="测试")
@ResponseBody
public String bootjson() {
return "这个是返回的数据";
}
@RequestMapping("/main")
@ApiOperation(value="主页",httpMethod="GET",notes="直接跳往主页")
public String main() {
log.info("开始往主业迈进");
return "main";
}
}
四、测试
浏览器输入 -> http://localhost:(项目的端口)/swagger-ui.html
五、错误解决
可能输入地址找不到,后台提示 No mapping for GET /swagger-ui.html
这个异常其实不用怎么解释,说白了就是找不到了。
这个时候我们配置一个东西使其重新指定静态资源
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {
/**
* 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations(
"classpath:/static/");
registry.addResourceHandler("swagger-ui.html").addResourceLocations(
"classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}