javascript
03、Swagger2和Springmvc整合详细记录(爬坑记录)
| 2018年6月18日 | 基本使用 | spirngmvc整合swagger2 |
開始之前這個(gè)系列博文基本是,在項(xiàng)目的使用中一些模塊的內(nèi)容記錄,但是后期逐漸優(yōu)化,不單單是整合內(nèi)容。
swagger簡(jiǎn)介
1、swagger主要提供了三個(gè)功能:
- Swagger Editor: Swagger提供的一個(gè)編輯器,用來(lái)通過(guò)Swagger提供的特定的YAML語(yǔ)法來(lái)編寫API文檔
- Swagger Codegen: 代碼生成器
- Swagger UI: YAML語(yǔ)法定義我們的RESTful API,然后它會(huì)自動(dòng)生成一篇排版優(yōu)美的API文檔,并且提供實(shí)時(shí)預(yù)覽。
spirngmv 整合swagger
其實(shí)這個(gè)整合例子甚多,可以選擇的使用。
推薦:官方的說(shuō)明文檔,雖是英文但是各個(gè)配置含義說(shuō)明的很清楚建議使用,后期的優(yōu)化內(nèi)容根據(jù)這個(gè)執(zhí)行的。
導(dǎo)入基本步驟:
1、修改pom.xml;添加如下swagger的依賴
<!-- 構(gòu)建Restful API --> <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> 復(fù)制代碼2、添加如下的配置類 注意如下不是必要的步驟(swagger會(huì)使用默認(rèn)配置)
package com.weir.utils;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; 2 (basePackages ="com.weir") class ApiConfig extends WebMvcConfigurationSupport {public Docket api() {return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.weir")).paths(PathSelectors.any()).build().apiInfo(apiInfo());}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("接口列表 v1.1.0") // 任意,請(qǐng)稍微規(guī)范點(diǎn).description("接口測(cè)試") // 任意,請(qǐng)稍微規(guī)范點(diǎn).termsOfServiceUrl("http://localhost:8080/swagger-ui.html") // 將“url”換成自己的ip:port.version("1.1.0").build();} } 復(fù)制代碼3、修改springmvc.xml文件;添加關(guān)于swagger的配置,內(nèi)容如下:
<mvc:default-servlet-handler /> <!-- 配置Swagger相關(guān)靜態(tài)資源 --> <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/> <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/> <!-- 添加掃描配置類 --> <bean class="com.weir.utils.ApiConfig" /> 復(fù)制代碼4、代碼中的使用:
@RequestMapping(value = "/login") @ApiOperation(value = "用戶登錄", notes = "用戶登錄操作") @ApiImplicitParam(name = "id", value = "用戶ID", required = true, dataType = "Long") @ResponseBody public ServerResponse<User> login(String userName, String password, HttpSession session){ServerResponse<User> response = userService.login (userName, password);if (response.isSuccess ()){session.setAttribute ("user",response);}return response; } 復(fù)制代碼5、運(yùn)行顯示
導(dǎo)入遇到問(wèn)題記錄:
最重要的內(nèi)容:(自己爬坑的內(nèi)容)【一定要注意】
1、控制臺(tái)報(bào)錯(cuò)內(nèi)容:Getting HTTP 404 error on /swagger-ui.html but other swagger endpoint works
攔截路徑改為/,不要配置成后綴的,例如:/*.do等,這會(huì)導(dǎo)致,wagger-ui.html頁(yè)面被攔截,無(wú)法加載。
<!-- dispatcherServlet --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param><param-name>contextConfigLocation</param-name><param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 復(fù)制代碼當(dāng)然如果你要配置成帶有后綴的也是可以的,請(qǐng)參考如下內(nèi)容:
- 解決的關(guān)鍵參考頁(yè)面 [github的issue]
- 關(guān)于配置成為的后綴的方式解決官方文檔
總結(jié):
- 他人博文只是具有參考意義,盡量參考官方說(shuō)明文檔,弄懂原理是關(guān)鍵
- 推薦官方文檔,中文文檔和博客參差不齊
- 不要位畏懼英文文檔和資料
總結(jié)
以上是生活随笔為你收集整理的03、Swagger2和Springmvc整合详细记录(爬坑记录)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot系列: Spring
- 下一篇: mac远程桌面Microsoft Rem