广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Swagger2配置方式(解决404报错)
  • 159
分享到

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

2024-04-02 19:04:59 159人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录swagger2配置(解决404报错)一、引入依赖二、编写配置文件三、解决404报错swagger配置好后仍然404问题Swagger2配置(解决404报错) 在Spring B

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报错,则进行下面的配置

在这里插入图片描述

三、解决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

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Swagger2配置方式(解决404报错)

本文链接: https://www.lsjlt.com/news/156698.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作