Swagger2配置(解决404报错)
在spring boot项目中配置Swagger2,配置好了但是访问确实404,SwaggerConfig中的注入方法也执行了还是访问不到页面。究其原因是MVC没有找到swagger-ui包中的swagger-ui.html文件。
Swagger2的配置步骤如下:
一、引入依赖
pom.wml
1
2
3
4
5
6
7
8
9
10
|
< 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 > |
二、编写配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
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 -> { // 循环判断匹配 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()); } } |
至此已经配置完成,启动项目访问 http://localhost: p o r t / {port}/ port/{context-path}/swagger-ui.html
如果访问成功则不需要继续下面的配置,如果访问失败出现404报错,则进行下面的配置
三、解决404报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
4
5
6
7
|
@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.有可能是 有其他类 实现了 WebMvcConfigurer 或者 继承了 WebMvcConfigurationSupport
导致的WebMvcConfigurationSupport 在继承的时候 没有重写addResourceHandlers
2.spring boot 启动模式有三种 如果默认没有改动的话 应该是SERVLET
-
NONE
-
SERVLET
-
REACTIVE
注意查看 只有SERVLET 会加载webmvc配置
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xfx_1994/article/details/106454363