日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

03、Swagger2和Springmvc整合详细记录(爬坑记录)

發(fā)布時間:2025/4/5 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 03、Swagger2和Springmvc整合详细记录(爬坑记录) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
時間內(nèi)容備注
2018年6月18日基本使用spirngmvc整合swagger2

開始之前這個系列博文基本是,在項目的使用中一些模塊的內(nèi)容記錄,但是后期逐漸優(yōu)化,不單單是整合內(nèi)容。

swagger簡介

1、swagger主要提供了三個功能:

  • Swagger Editor: Swagger提供的一個編輯器,用來通過Swagger提供的特定的YAML語法來編寫API文檔
  • Swagger Codegen: 代碼生成器
  • Swagger UI: YAML語法定義我們的RESTful API,然后它會自動生成一篇排版優(yōu)美的API文檔,并且提供實時預(yù)覽。

spirngmv 整合swagger

其實這個整合例子甚多,可以選擇的使用。

推薦:官方的說明文檔,雖是英文但是各個配置含義說明的很清楚建議使用,后期的優(yōu)化內(nèi)容根據(jù)這個執(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會使用默認配置)

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;@Configuration @EnableSwagger2 @EnableWebMvc @ComponentScan(basePackages ="com.weir") class ApiConfig extends WebMvcConfigurationSupport {@Beanpublic 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") // 任意,請稍微規(guī)范點.description("接口測試") // 任意,請稍微規(guī)范點.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、運行顯示

導(dǎo)入遇到問題記錄:

最重要的內(nèi)容:(自己爬坑的內(nèi)容)【一定要注意

1、控制臺報錯內(nèi)容:Getting HTTP 404 error on /swagger-ui.html but other swagger endpoint works

攔截路徑改為/,不要配置成后綴的,例如:/*.do等,這會導(dǎo)致,wagger-ui.html頁面被攔截,無法加載。

<!-- 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)然如果你要配置成帶有后綴的也是可以的,請參考如下內(nèi)容:

  • 解決的關(guān)鍵參考頁面 [github的issue]
  • 關(guān)于配置成為的后綴的方式解決官方文檔

總結(jié):

  • 他人博文只是具有參考意義,盡量參考官方說明文檔,弄懂原理是關(guān)鍵
  • 推薦官方文檔,中文文檔和博客參差不齊
  • 不要位畏懼英文文檔和資料

總結(jié)

以上是生活随笔為你收集整理的03、Swagger2和Springmvc整合详细记录(爬坑记录)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。