swagger是用来自动生成api文档的工具,还能兼测试请求的工具
首先在maven中导入以下依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.7</version>
</dependency>
创建一个swagger-config用来配置信息
@Configuration// 写了applicationContext-mybatis.xml
@ComponentScan(basePackages="controller") // <context-ComponentScan basepackage="xxx.xx.x.xx">
@EnableSwagger2//启动swaggerapi生成
public class SwaggerConfig extends WebMvcConfigurationSupport{
@Bean// <bean id="apiGen" class="Docket"/>
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
/**
* 创建该Api的基本信息(这些基本信息会展现在文档页面中)
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("这是标题")
.termsOfServiceUrl("")
.contact("这是小组")
.version("这是版本")
.build();
}
}
需要在spring-mvc的配置文件中指定Swagger-config工具文件的路径
<bean class="util.SwaggerConfig"/>
最后在网页输入地址+后缀(本地测试: http://localhost:8080/swagger-ui.html)