---
title: "swagger2与springmvc的整合"
author: "lfwer"
date: "2018-07-17T14:44:12Z"
tags: []
categories: ["编程语言"]
slug: "swagger2-springmvc"
draft: false
lastmod: "2018-07-17T14:44:12Z"
id: 2427008
url: "https://lfwer.iteye.com/blog/2427008"
---

1 添加如下依赖：

```
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
```

 

2 增加Swagger2Config配置类，只显示使用@Api注解的api。

```
@Component
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackageClasses = QuartzManagerAction.class)
public class Swagger2Config {
@Bean
public Docket createAPI() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
Contact contact = new Contact("lfw", "http://lfwer.iteye.com", "lfwer@163.com");
ApiInfo apiInfo = new ApiInfoBuilder().license("Apache License Version 2.0").title("定向流量系统").description("Swagger API").contact(contact).version("1.0").build();
return apiInfo;
}
}
```

  

3 修改springmvc-servlet.xml，增加swagger静态资源文件路径配置

```
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
```