欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Swagger2配置方式(解決404報(bào)錯(cuò))

 更新時(shí)間:2021年11月08日 10:59:45   作者:三毛村滴雪魚粉  
這篇文章主要介紹了Swagger2配置方式(解決404報(bào)錯(cuò)),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Swagger2配置(解決404報(bào)錯(cuò))

在spring boot項(xiàng)目中配置Swagger2,配置好了但是訪問確實(shí)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)配置完成,啟動(dòng)項(xiàng)目訪問 http://localhost: p o r t / {port}/ port/{context-path}/swagger-ui.html

如果訪問成功則不需要繼續(xù)下面的配置,如果訪問失敗出現(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 對應(yīng)的文件

swagger配置好后仍然404問題

記錄一下 學(xué)習(xí)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.有可能是 有其他類 實(shí)現(xiàn)了 WebMvcConfigurer 或者 繼承了 WebMvcConfigurationSupport

導(dǎo)致的WebMvcConfigurationSupport 在繼承的時(shí)候 沒有重寫addResourceHandlers

2.spring boot 啟動(dòng)模式有三種 如果默認(rèn)沒有改動(dòng)的話 應(yīng)該是SERVLET

  • NONE
  • SERVLET
  • REACTIVE

注意查看 只有SERVLET 會(huì)加載webmvc配置

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論