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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springmvc+swagger2

發布時間:2025/3/8 c/c++ 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springmvc+swagger2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、swagger2依賴

<!--swagger--> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><exclusions><exclusion><artifactId>spring-aop</artifactId><groupId>org.springframework</groupId></exclusion><exclusion><artifactId>jackson-annotations</artifactId><groupId>com.fasterxml.jackson.core</groupId></exclusion></exclusions> </dependency> <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId> </dependency>

二、springmvc配置文件加入

<mvc:default-servlet-handler />

三、web.xml配置

<welcome-file-list><welcome-file>swagger-ui.html</welcome-file> </welcome-file-list>

四、swagger2配置

  可創建多個Docket,對restful api進行分組管理

import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.context.annotation.Bean; 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;@EnableWebMvc @EnableSwagger2 @Configuration public class Swagger2Config extends WebMvcConfigurationSupport {@Beanpublic Docket docket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("admin").select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.ant("/**")).build();}// @Bean // public Docket xxx() { // return new Docket(DocumentationType.SWAGGER_2) // .apiInfo(apiInfo()) // .groupName("xxx") // .select() // .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // .paths(PathSelectors.ant("/xxx/**")) // .build(); // }private ApiInfo apiInfo() {return new ApiInfoBuilder().title("shiro 無狀態組件").contact(new Contact("胡俊哲個", "", "2570230521@qq.com")).version("1.0").build();} }

五、效果演示

Restful API 訪問路徑: * http://IP:port/{context-path}/swagger-ui.html

六、注意事項

  1、如果有攔截器或者過濾器 對項目根路徑進行攔截,可能<welcome-file>的配置不生效!

  2、如果 配置?<welcome-file>swagger-ui.html</welcome-file>,訪問?http://IP:port/{context-path}/,雖然可以看到頁面,但是接口內容顯示不出來。原因如下:

  

  這個js在匹配url的時候 要包含“swagger-ui.html”,所以就出錯了。解決方案如下:

  (1)修改web.xml

<welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>

  (2)新建index.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>首頁</title> </head> <body> <script type="application/javascript">window.location.href = window.location.origin + "/shiro/swagger-ui.html"; </script> </body> </html>

七、swagger2常用注解參考

  Swagger-Core Annotations

轉載于:https://www.cnblogs.com/hujunzheng/p/7210140.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的springmvc+swagger2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。