Swagger2配置方式(解決404報(bào)錯(cuò))
Swagger2配置(解決404報(bào)錯(cuò))
在spring boot項(xiàng)目中配置Swagger2,配置好了但是訪問(wèn)確實(shí)404,SwaggerConfig中的注入方法也執(zhí)行了還是訪問(wèn)不到頁(yè)面。究其原因是MVC沒(méi)有找到swagger-ui包中的swagger-ui.html文件。
Swagger2的配置步驟如下:
一、引入依賴(lài)
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>
二、編寫(xiě)配置文件
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")) //這里采用包掃描的方式來(lái)確定要顯示的接口
// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //這里采用包含注解的方式來(lái)確定要顯示的接口
.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)目訪問(wèn) http://localhost: p o r t / {port}/ port/{context-path}/swagger-ui.html
如果訪問(wèn)成功則不需要繼續(xù)下面的配置,如果訪問(wèn)失敗出現(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 對(duì)應(yīng)的文件
swagger配置好后仍然404問(wèn)題
記錄一下 學(xué)習(xí)spring boot 遇到的問(wèn)題
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.有可能是 有其他類(lèi) 實(shí)現(xiàn)了 WebMvcConfigurer 或者 繼承了 WebMvcConfigurationSupport
導(dǎo)致的WebMvcConfigurationSupport 在繼承的時(shí)候 沒(méi)有重寫(xiě)addResourceHandlers
2.spring boot 啟動(dòng)模式有三種 如果默認(rèn)沒(méi)有改動(dòng)的話 應(yīng)該是SERVLET
NONESERVLETREACTIVE
注意查看 只有SERVLET 會(huì)加載webmvc配置
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot3.1.2 引入Swagger報(bào)錯(cuò)Type javax.servlet.http.HttpServletRequest not present解決辦法
- Springboot整合Swagger2后訪問(wèn)swagger-ui.html 404報(bào)錯(cuò)問(wèn)題解決方案
- SpringBoot引入swagger報(bào)錯(cuò)處理的解決方法
- Spring?Boot?2.6.x整合Swagger啟動(dòng)失敗報(bào)錯(cuò)問(wèn)題的完美解決辦法
- 關(guān)于springboot集成swagger3時(shí)spring-plugin-core報(bào)錯(cuò)的問(wèn)題
- 解決swagger主頁(yè)訪問(wèn),返回報(bào)錯(cuò)500問(wèn)題
相關(guān)文章
java發(fā)送短信系列之限制日發(fā)送次數(shù)
這篇文章主要為大家詳細(xì)介紹了java發(fā)送短信系列之限制日發(fā)送次數(shù),詳細(xì)介紹了限制每日向同一個(gè)用戶(根據(jù)手機(jī)號(hào)和ip判斷)發(fā)送短信次數(shù)的方法,感興趣的小伙伴們可以參考一下2016-02-02
PowerJob的ServerDiscoveryService工作流程源碼解讀
java Callable與Future的詳解及實(shí)例
springboot?集成easy-captcha實(shí)現(xiàn)圖像驗(yàn)證碼顯示和登錄
Java開(kāi)發(fā)之spring security實(shí)現(xiàn)基于MongoDB的認(rèn)證功能

