1. 程式人生 > >SpringMVC與Springfox(Swagger2)整合詳解以及涉及的問題處理

SpringMVC與Springfox(Swagger2)整合詳解以及涉及的問題處理

        Swagger 是一個規範和完整的框架,用於生成、描述、呼叫和視覺化 RESTful 風格的 Web 服務。總體目標是使客戶端和檔案系統作為伺服器以同樣的速度來更新。檔案的方法,引數和模型緊密整合到伺服器端的程式碼,允許API來始終保持同步。

 作用:

         1. 介面的文件線上自動生成。

        2. 功能測試。

       在做Spring+SpringMVC+Mybatis的專案中整合Springfox(Swagger2)。  在網上查看了很多的資料,遇到了好多坑點,

摸索了多半天,總算是成功的整合了。現在記錄一下成果,希望能給有需求的朋友提供參考,就可以早點回家吃飯了

        今天不講SSM整合,是在這個基礎上整合springfox,我的Spring版本是4.0.9.RELEASE,springfox用的是2.4.0(spring版本要和springfox對應,高版本的springfox裡面有些方法低版本的Spring沒有,我們工作環境是spring4.0.9,所以為以下示例,但是spring4.3.5+與springfox2.7.0整合也是可以的),如果使用FastJson的話版本得在1.2.10以上(不然http://localhost:8080/v2/api-docs

返回為空),我用的是1.2.30。

 

1、在maven的pom檔案中引入springfox的依賴

		<!-- swagger -->
		<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.fasterxml.jackson.core</groupId>  
                    <artifactId>jackson-databind</artifactId>  
                    <version>2.6.3</version>  
                </dependency>  
                <!-- FastJson的版本必須在1.2.10以上-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.30</version>
		</dependency>

2、在原始碼目錄下建立一個單獨的package,然後建立Swagger2Config.java檔案

package com.common;
 
import org.springframework.context.annotation.Bean;
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;
 
@EnableSwagger2
public class Swagger2Config {
	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
			.apiInfo(apiInfo())
  			.enable(true)  //是否啟用Swagger
			.select()
			.apis(RequestHandlerSelectors.basePackage("com.group.controller")) //掃描的包
			.paths(PathSelectors.any())
			.build();
	}
 
	private ApiInfo apiInfo() {
 
		return new ApiInfoBuilder()
			.title("xxx測試服務API")
			.description("杭州xx網路科技@API")
			.termsOfServiceUrl("http://xxx.cc/")
			.license("© 2018-chenwei. All rights reserved.")
			.version("1.0")
			.build();
	}
}

3、在springMVC的配置檔案中配置swagger

	<!--新增swagger2配置-->
	<!-- API訪問地址:http://ip:port/swagger-ui.html -->
	<bean class="com.common.Swagger2Config" />
	<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
	<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>

4、修改web.xml檔案中配置所有的請求都經DispatcherServlet處理

<servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

注意:這個地方必須配置,如果你配置的是*.XXX的形式會出現api-docs訪問出錯,這就會導致swagger-ui找不到api的有效路徑。使swagger無法正常工作 

5、controller的配置,這裡我只做簡單的配置測試swagger是否正常工作

啟動專案:能夠正常的工作