软件编程
位置:首页>> 软件编程>> java编程>> Swagger2配置方式(解决404报错)

Swagger2配置方式(解决404报错)

作者:三毛村滴雪鱼粉  发布时间:2022-08-30 17:21:13 

标签:Swagger2,配置,404

Swagger2配置(解决404报错)

在spring boot项目中配置Swagger2,配置好了但是访问确实404,SwaggerConfig中的注入方法也执行了还是访问不到页面。究其原因是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 -> {
           // 循环判断匹配
           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报错,则进行下面的配置

Swagger2配置方式(解决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.有可能是 有其他类 实现了 WebMvcConfigurer 或者 继承了 WebMvcConfigurationSupport

导致的WebMvcConfigurationSupport 在继承的时候 没有重写addResourceHandlers

2.spring boot 启动模式有三种 如果默认没有改动的话 应该是SERVLET

  • NONE

  • SERVLET

  • REACTIVE

注意查看 只有SERVLET 会加载webmvc配置

来源:https://blog.csdn.net/xfx_1994/article/details/106454363

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com