<properties> <jackson2.version>2.5.1</jackson2.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.2.6.RELEASE</spring.version> <!-- swagger --> <swagger.version>2.6.1</swagger.version> </properties> <dependencies> <!-- servlet Start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet_version}</version> <scope>provided</scope> </dependency> <!--END servlet END --> <!-- test start --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit_version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- end of test --> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring end --> <!-- common start --> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>${commons_configuration}</version> <!-- <type>jar</type> <scope>compile</scope> --> </dependency> <!-- end of common --> <!-- jackson json 處理包--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson2.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson2.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson2.version}</version> </dependency> <!-- jackson json 處理包 END --> <!-- Log libs --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>${log.janino.version}</version> <type>jar</type> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j_version}</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j_version}</version> </dependency> <!-- end log --> <!-- springfox START --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <!-- springfox END --> <!--petstore是官方的一個demo,加入此依賴是爲了稍後參考接口描述的編寫--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-petstore</artifactId> <version>${swagger.version}</version> </dependency> <!--解決tomcat部署跨域問題 --> <dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>cors-filter</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>com.thetransactioncompany</groupId> <artifactId>java-property-utils</artifactId> <version>1.9.1</version> </dependency> </dependencies> <!-- 配置編譯插件和tomcat 環境 --> <build> <finalName>las</finalName> <plugins> <!-- Force Java 7 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <path> /${project.build.finalName} </path> <uriEncoding>UTF-8</uriEncoding> <port>9999</port> </configuration> </plugin> </plugins> </build>
<!-- 注意component-scan 掃描路徑 --> <context:component-scan base-package="com.xxxx.las.web.control"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> <!-- Scan springfox demo --> <context:component-scan base-package="springfox.petstore.controller"/>
package com.xxxx.las.web.control.swagger; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Component @EnableSwagger2 public class ApplicationSwaggerConfig { @Bean public Docket addUserDocket() { Docket docket = new Docket(DocumentationType.SWAGGER_2); ApiInfo apiInfo = new ApiInfo( "終端 API", "API Document管理", "V1.0.0", "www.xxxx.com", "759534585@qq.com", "", ""); docket.apiInfo(apiInfo); return docket; } } //以上配置項目能夠修改
<!--解決tomcat部署跨域問題 --> <filter> <filter-name>CORS</filter-name> <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class> <init-param> <param-name>cors.allowOrigin</param-name> <param-value>*</param-value> </init-param> <init-param> <param-name>cors.supportedMethods</param-name> <param-value>GET, POST, HEAD, PUT, DELETE</param-value> </init-param> <init-param> <param-name>cors.supportedHeaders</param-name> <param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value> </init-param> <init-param> <param-name>cors.exposedHeaders</param-name> <param-value>Set-Cookie</param-value> </init-param> <init-param> <param-name>cors.supportsCredentials</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CORS</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
支持發佈服務跨域請求的方式有多種、以上採用第三方已經提供好的解決方式。 注意:在線上環境請將以上配置去掉。javascript
mvn clean tomcat7:run 接口服務地址爲:http://localhost:9999/lashtml
springfox暴露的接口路徑爲:/v2/api-docs前端
接口已經暴露了。稍後介紹經過使用swagger-ui展現接口。java
下載路徑:swagger-ui源代碼nginx
行號:29 行git
對比使用以下代碼支持中文顯示:github
<!-- Some basic translations --> <script src='lang/translator.js' type='text/javascript'></script> <!-- <script src='lang/ru.js' type='text/javascript'></script> --> <!-- <script src='lang/en.js' type='text/javascript'></script> --> <script src='lang/zh-cn.js' type='text/javascript'></script>
效果圖以下: web
紅色框體輸入框:輸入你後臺的接口地址路徑 http://ip:port/{context}/v2/api-docsspring
在swagger-ui首頁輸入框中輸入以上啓動的後臺服務接口路徑以下: http://localhost:9999/las/v2/api-docs 效果以下: express
能夠經過springfox官網提供的實例代碼學習: springfox-petstore 下一篇文章將詳細介紹如何使用swagger-annotation讓springmvc的接口直接暴露出來能夠經過swagger-ui查看測試。