Swagger2配置方式(解決404報錯)
Swagger2配置(解決404報錯)
在spring boot項目中配置Swagger2,配置好了但是訪問確實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)配置完成,啟動項目訪問 http://localhost: p o r t / {port}/ port/{context-path}/swagger-ui.html
如果訪問成功則不需要繼續(xù)下面的配置,如果訪問失敗出現(xiàn)404報錯,則進行下面的配置

三、解決404報錯
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 對應的文件
swagger配置好后仍然404問題
記錄一下 學習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.有可能是 有其他類 實現(xiàn)了 WebMvcConfigurer 或者 繼承了 WebMvcConfigurationSupport
導致的WebMvcConfigurationSupport 在繼承的時候 沒有重寫addResourceHandlers
2.spring boot 啟動模式有三種 如果默認沒有改動的話 應該是SERVLET
NONESERVLETREACTIVE
注意查看 只有SERVLET 會加載webmvc配置
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java發(fā)送短信系列之限制日發(fā)送次數(shù)
這篇文章主要為大家詳細介紹了java發(fā)送短信系列之限制日發(fā)送次數(shù),詳細介紹了限制每日向同一個用戶(根據(jù)手機號和ip判斷)發(fā)送短信次數(shù)的方法,感興趣的小伙伴們可以參考一下2016-02-02
PowerJob的ServerDiscoveryService工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的ServerDiscoveryService工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
springboot?集成easy-captcha實現(xiàn)圖像驗證碼顯示和登錄
本文主要介紹了springboot?集成easy-captcha實現(xiàn)圖像驗證碼顯示和登錄,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
Java開發(fā)之spring security實現(xiàn)基于MongoDB的認證功能
這篇文章主要介紹了Java開發(fā)之spring security實現(xiàn)基于MongoDB的認證功能,結(jié)合實例形式分析了spring security在非JDBC環(huán)境下的自定義認證服務實現(xiàn)技巧,需要的朋友可以參考下2017-11-11

