springboot整合swagger2访问swagger-ui.html出现404解决方案

直接上pom需要的依赖


            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

然后书写配置类

package com.shengxi.wangyang.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author y
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                #一般就是对应的Controller的路径
                .apis(RequestHandlerSelectors.basePackage("com.**.wangyang.app"))
                .paths(PathSelectors.regex("/.*"))
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("matthew swagger-bootstrap-ui RESTful APIs")
                .description("matthew swagger-bootstrap-ui")
                .contact(new Contact("matthew", "matthew.com", "[email protected]"))
                .version("7.4")
                .build();

    }
}

大部分人这样就可以直接使用swagger2的接口文档了,但是我属于代码见我就欺负我系列的,swagger-ui.html访问的时候会出现404,原因是因为对应的静态资源加载不到。我们需要手动加一个静态资源的配置类。

package com.shengxi.wangyang.common.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 重新指定静态资源
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
        // 解决 SWAGGER2 404报错
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 配置servlet处理
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

注意静态资源的路径是不需要修改的,因为那是jar包里面的页面不是的自己编写的,所以不会出现页面路径错误问题。

你可能感兴趣的:(web开发笔记,spring,boot,项目实战)