目的
前后端分离开发时,双方开发人员需要以接口文档为开发导向。
通过swagger2可以按以下思路进行工作:
首先,先在controller层把接口方法定义好,并添加相应的注释和接口说明,参数说明等。
再通过swagger2自动生成相应的接口说明文档页面,输出给前端开发人员。
这样可以减少重复编写文档的工作,并且在对接口后续修改的同时更新文档。
具体步骤
- 在工程的pom中引入swagger2的依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
- 添加配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
// 自行修改为自己的包路径
.apis(RequestHandlerSelectors.basePackage("com.ldsj.bridging.controller"))
.paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("API文档标题").description("API文档描述")
// 服务条款网址
// .termsOfServiceUrl("http://www.xxxx.com/terms")
.version("1.0") //版本
// .contact(new Contact("作者联系人", "url", "email"))
.build();
}
}
- 启动应用并访问接口文档地址
http://localhost:8080/swagger-ui.html

问题
有可能在打开以上路径的时候出现404页面无法找到
检查一下项目配置文件中是否有以下
spring:
mvc:
static-path-pattern: /static/**
如果有就删除或者注释掉即可

被折叠的 条评论
为什么被折叠?



