# Swagger2 SpringMVC集成(非SpringBoot)
Swgger
做過(guò)手機(jī)端接口的同學(xué)們都有過(guò)歷史的經(jīng)歷,寫(xiě)完一個(gè)接口之后,Junit測(cè)試調(diào)試過(guò)后是偷,需要在接口文檔中詳細(xì)的去編寫(xiě)給客戶端開(kāi)發(fā)同學(xué)看的一份文檔淌实,而且稍微不注意就有可能寫(xiě)錯(cuò)一個(gè)字母,下面來(lái)段單口相聲(PS: 不會(huì)單口相聲的程序員不是好產(chǎn)品T桨堋4ビ住!)
場(chǎng)景:APP活動(dòng)接口
任務(wù):我究飞,APP開(kāi)發(fā)者
時(shí)間:及其困乏的下午
我:Hi置谦,哥們,接口開(kāi)發(fā)好了亿傅,文檔已經(jīng)在git上面更新媒峡。
APP開(kāi)發(fā)者:恩,我來(lái)請(qǐng)求一下葵擎。
......大約半分鐘后...
APP開(kāi)發(fā)者:接口請(qǐng)求不到谅阿。
我:(內(nèi)心:怎么可能,我都單元測(cè)試過(guò)了坪蚁,我確信我看的都是綠的啊奔穿,綠的啊,)你是不是寫(xiě)的有問(wèn)題敏晤,自己檢查一下哈(PS:怎么可能是我的問(wèn)題)贱田。
........大約一分鐘后......
APP開(kāi)發(fā)者:請(qǐng)求不到。
APP開(kāi)發(fā)者:請(qǐng)求不到嘴脾。
APP開(kāi)發(fā)者: 請(qǐng)求不到男摧。
我: 我來(lái)看一下(PS :我也是客戶端開(kāi)發(fā)者)蔬墩,沒(méi)什么問(wèn)題,怎么就請(qǐng)求不到呢耗拓,好吧我笑而不語(yǔ)的把/INfo 修改成/info SUCCESS !
也許上面的經(jīng)歷你也經(jīng)歷過(guò)拇颅,我也只是說(shuō)明一下有這么中情況,事情是真實(shí)的乔询,劇情嘛~ 每一個(gè)程序員都是一個(gè)優(yōu)秀的導(dǎo)演樟插,哈哈哈 ~ 下面切入整體,介紹一下怎么把Swagger2 和 SpringMVC 結(jié)合竿刁,看到最后的結(jié)果黄锤,你就知道Swagger2的有點(diǎn)了。
Swagger官網(wǎng)
集成步驟
- 構(gòu)建Maven工程
- 添加Swagger2依賴
<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>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.6</version>
</dependency>
* 配置Swagger2
@Component
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.zhaoshuai.controller")
public class Swagger2Config {
@Bean
public Docket createAPI() {
return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
//過(guò)濾生成鏈接
.paths(PathSelectors.any()).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
Contact contact=new Contact("趙帥","http://blog.maileba.top","zhaoshuaivov@163.com");
ApiInfo apiInfo = new ApiInfoBuilder().license("Apache License Version 2.0").title("Swagger 集成測(cè)試").description("Swagger API Teste").contact(contact).version("1.0").build();
return apiInfo;
}
}
* 設(shè)置spring-mvc.xml
<mvc:default-servlet-handler />
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
* 驗(yàn)證 http://ip:prot/project_name/swagger-ui.html 如果沒(méi)有project_name 則省去
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/1477250-fe043120f9c8e279.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
###附錄
* pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven- 4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Swagger</groupId>
<artifactId>swagger</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/asm/asm -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/asm/asm-commons -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm-commons</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
</dependencies>
<repositories><!-- 代碼庫(kù) -->
<!--<repository>-->
<!--<id>maven-ali</id>-->
<!--<url>http://maven.aliyun.com/nexus/content/groups/public//</url>-->
<!--<releases>-->
<!--<enabled>true</enabled>-->
<!--</releases>-->
<!--<snapshots>-->
<!--<enabled>true</enabled>-->
<!--<updatePolicy>always</updatePolicy>-->
<!--<checksumPolicy>fail</checksumPolicy>-->
<!--</snapshots>-->
<!--</repository>-->
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<layout>default</layout>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>