Spring 4.1.2更新后找不到资源

时间:2014-12-03 17:23:48

标签: java spring spring-mvc

将我的Web应用程序从Spring 4.1.0更新到4.1.2后,资源映射停止工作。

没有任何更改,对任何资源的每个请求都返回“HTTP 404 - NOT FOUND”(。js,.png等等)。

然后我切换回Spring 4.1.0,一切都恢复正常。

这是我的应用程序配置类:

public class MvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/**");
    }

}

这个问题类似于:Resource Not Found after Spring 4.1.2 Update when deploy with JRebel 6.0.0

我的资源位于:

  • [项目] \的WebContent \资源

这是我的servlet初始化程序:

public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements HttpSessionListener {

    private static final int MAX_UPLOAD_SIZE = 1 * 1024 * 1024; // 1mb;

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { MvcConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
        encodingFilter.setEncoding("UTF-8");
        encodingFilter.setForceEncoding(true);

        return new Filter[] { encodingFilter, new MultipartFilter() };
    }

    @Override
    protected void customizeRegistration(Dynamic registration) {
        File uploadDirectory = new File(System.getProperty("java.io.tmpdir"));
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement(uploadDirectory.getAbsolutePath(), MAX_UPLOAD_SIZE, MAX_UPLOAD_SIZE * 2, MAX_UPLOAD_SIZE / 2);
        registration.setMultipartConfig(multipartConfigElement);
    }

1 个答案:

答案 0 :(得分:1)

只需将资源处理程序更改为以下内容:

public class MvcConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

从Spring 4.1.1升级时,这也破坏了我的代码,但是在Spring 4.1.6中删除了尾随的/**修复了这个问题。