扩展 Spring Boot WebMvcConfigurationSupport 时静态资源的问题 [英] Issue with Static resources when extending Spring Boot WebMvcConfigurationSupport

查看:88
本文介绍了扩展 Spring Boot WebMvcConfigurationSupport 时静态资源的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我扩展了 WebMvcConfigurationSupport 以实现 api 版本控制方案 - 即

I extended WebMvcConfigurationSupport to implement an api versioning scheme - i.e.

@Configuration
public class ApiVersionConfiguration extends WebMvcConfigurationSupport {

    @Override
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new ApiVersionRequestMappingHandlerMapping(readDateToVersionMap());
    }}

这使用自定义处理程序映射来版本 api 并且工作得很好.

This uses a custom handler mapping to version the api and works quite nicely.

但是,它似乎也禁用了 @EnableAutoConfiguration bean,因此现在不提供静态资源(如本问题所述 是否可以扩展 WebMvcConfigurationSupport 并使用 WebMvcAutoConfiguration?).

However it also seems to disable the @EnableAutoConfiguration bean so that now static resources aren't served (as mentioned in this question Is it possible to extend WebMvcConfigurationSupport and use WebMvcAutoConfiguration?).

好吧,我想,让我们向上面的类添加一个资源处理程序 - 即

Ok, I thought, let's just add a resource handler to the class above - i.e.

@Configuration
public class ApiVersionConfiguration extends WebMvcConfigurationSupport {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("classpath:/public/").addResourceLocations("/");
    }

    @Override
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        return new ApiVersionRequestMappingHandlerMapping(readDateToVersionMap());
    }}

但是..这不起作用..?当我浏览到/index.html 时出现此错误:

However.. this isn't working..? I get this error when I browse to /index.html:

No mapping found for HTTP request with URI [/index.html] in DispatcherServlet with name 'dispatcherServlet' 

..如果我禁用这个类,那么@EnableAutoConfiguration 魔法可以很好地提供这些资源.

..If I disable this class then these resources are served just fine by @EnableAutoConfiguration magic.

我一直在使用各种选项来提供扩展 WebMvcConfigurationSupport 的静态内容,但迄今为止没有成功.

I've been playing with various options to serve static content having extended the WebMvcConfigurationSupport and thus far no success.

有什么想法吗?

推荐答案

我遇到了同样的问题,并想出了一个适合我的解决方案.如果您只想让资源正常工作而不必担心重复,您可以这样做:

I was facing the same problem and came up with a solution that just works for me. If you just want to get the resources working without worrying of repetition you can do:

@Configuration
public class StaticResourcesConfig extends WebMvcAutoConfigurationAdapter {

}

然后

@Configuration
@EnableWebMvc
@Import(StaticResourcesConfig.class)
public class WebConfig extends WebMvcConfigurationSupport {
    ...
}

只要您不在控制器中映射 /**,这就成功地使用 Spring Boot 默认值来提供静态资源.

This successfully uses the Spring Boot defaults for serving static resources, as long as you don't map /** in your controllers.

这篇关于扩展 Spring Boot WebMvcConfigurationSupport 时静态资源的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆