Spring boot 初学

springboot启用Swagger2导致静态资源404

2019-01-06  本文已影响785人  solarflare

最近在写一个spring boot项目,把其中一个大坑记录一下。
启用Swagger2时一切都按照教程来的,结果出了意想不到的错误。
首先添加dependency:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

然后配置SwaggerConfig

package com.example.myproject.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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 extends WebMvcConfigurationSupport {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(metaData());
    }
    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("career-spring")
                .description("\"Spring Boot - description\"")
                .version("1.0.0")
                .build();
    }
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

启动项目之后访问http://localhost:8080/swagger-ui.html,swagger ui看起来挺正常。但是/resources/static下的静态资源404了。
例如这个资源:\src\main\resources\static\js\vue.js,原本可以通过访问http://localhost:8080/js/vue.js看到,但现在是这样:

http://localhost:8080/js/vue.js成了这样

查阅了很多中英文资料,最后确定问题出在protected void addResourceHandlers(ResourceHandlerRegistry registry) 这里。csdn上大多数博客会建议改成这个样子,显然在相互“借鉴”:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**") .addResourceLocations("classpath:/META-INF/resources/")
            .setCachePeriod(0);
}

然而这不是静态资源404的解决方案,而是http://localhost:8080/swagger-ui.html出现404时的解决方案(并且还不是一个好方案)。
解决方法是在

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("/webjars/**")
           .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

的最上面添上一行,变成这样:

@Override
protected 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/");
}

重新启动项目,再访问http://localhost:8080/js/vue.js,发现恢复正常了:

http://localhost:8080/js/vue.js已恢复正常

http://localhost:8080/swagger-ui.html没有受影响,依然能正常访问。

上一篇 下一篇

猜你喜欢

热点阅读