Swagger2配置方式(解決404報(bào)錯(cuò))
Swagger2配置(解決404報(bào)錯(cuò))
在spring boot項(xiàng)目中配置Swagger2,配置好了但是訪問確實(shí)404,SwaggerConfig中的注入方法也執(zhí)行了還是訪問不到頁面。究其原因是MVC沒有找到swagger-ui包中的swagger-ui.html文件。
Swagger2的配置步驟如下:
一、引入依賴
pom.wml
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
二、編寫配置文件
package io.github.talelin.latticy.config; import com.google.common.base.Function; import com.google.common.base.Optional; import com.google.common.base.Predicate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { // 定義分隔符 private static final String splitor = ";"; @Bean Docket docket() { System.out.println("Swagger==========================================="); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(basePackage("io.github.talelin.latticy.controller.v1")) //這里采用包掃描的方式來確定要顯示的接口 // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //這里采用包含注解的方式來確定要顯示的接口 .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("CMS") .description("電商小程序 CMS Api文檔") .termsOfServiceUrl("https://blog.csdn.net/xfx_1994") .version("1.0") .build(); } public static Predicate <RequestHandler> basePackage(final String basePackage) { return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true); } private static Function <Class<?>, Boolean> handlerPackage(final String basePackage) { return input -> { // 循環(huán)判斷匹配 for (String strPackage : basePackage.split(splitor)) { boolean isMatch = input.getPackage().getName().startsWith(strPackage); if (isMatch) { return true; } } return false; }; } private static Optional<? extends Class<?>> declaringClass(RequestHandler input) { return Optional.fromNullable(input.declaringClass()); } }
至此已經(jīng)配置完成,啟動(dòng)項(xiàng)目訪問 http://localhost: p o r t / {port}/ port/{context-path}/swagger-ui.html
如果訪問成功則不需要繼續(xù)下面的配置,如果訪問失敗出現(xiàn)404報(bào)錯(cuò),則進(jìn)行下面的配置
三、解決404報(bào)錯(cuò)
package io.github.talelin.latticy.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebMvcConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } }
原理就是幫助MVC找到 swagger-ui.html 及其 CSS,JS 對應(yīng)的文件
swagger配置好后仍然404問題
記錄一下 學(xué)習(xí)spring boot 遇到的問題
swagger2
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); }
swagger 添加此配置之后仍然404
1.有可能是 有其他類 實(shí)現(xiàn)了 WebMvcConfigurer 或者 繼承了 WebMvcConfigurationSupport
導(dǎo)致的WebMvcConfigurationSupport 在繼承的時(shí)候 沒有重寫addResourceHandlers
2.spring boot 啟動(dòng)模式有三種 如果默認(rèn)沒有改動(dòng)的話 應(yīng)該是SERVLET
NONE
SERVLET
REACTIVE
注意查看 只有SERVLET 會(huì)加載webmvc配置
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA 自定義方法注解模板的實(shí)現(xiàn)方法
這篇文章主要介紹了IDEA 自定義方法注解模板的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09解決Java變異出現(xiàn)錯(cuò)誤No enclosing instance of type XXX is accessible
這牌你文章主要給大家分享解決Java變異出現(xiàn)錯(cuò)誤,具體的饑餓絕方案請看下面文章的內(nèi)容,需要的朋友可以參考一下,希望能幫助到你2021-09-09Java中的for循環(huán)結(jié)構(gòu)及實(shí)例
這篇文章主要介紹了Java中的for循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java中的while無限循環(huán)結(jié)構(gòu)及實(shí)例
這篇文章主要介紹了Java中的while無限循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01手把手教你如何用JAVA連接MYSQL(mysql-connector-j-8.0.32.jar)
這篇文章主要介紹了關(guān)于如何用JAVA連接MYSQL(mysql-connector-j-8.0.32.jar)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01Java核心編程之文件隨機(jī)讀寫類RandomAccessFile詳解
這篇文章主要為大家詳細(xì)介紹了Java核心編程之文件隨機(jī)讀寫類RandomAccessFile,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08