首页 > 解决方案 > @EnableWebMvc 和 WebMvcConfigurationSupport 的问题

问题描述

我创建了一个 Spring Boot 应用程序,我正在尝试从磁盘上的外部文件夹中获取图像,这是我的代码:

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

 @Configuration
 @EnableWebMvc
 @ComponentScan
 public class WebConfigurer implements WebMvcConfigurer {
 private static String UPLOAD_DIR = "D:\\upload";


@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/images/**").addResourceLocations("file:" + UPLOAD_DIR+"/");
}
}

但是当我启动应用程序时,我收到了以下错误消息:

> o.s.web.servlet.PageNotFound: No mapping for GET /myapp/index
> o.s.b.w.servlet.support.ErrorPageFilter  : Cannot forward to error
> page for request [/] as the response has already been committed.

即使我删除 @EnableWebMvc 并直接扩展 WebMvcConfigurationSupport,我也会遇到同样的问题。

  1. 为什么我会遇到这个问题?

  2. 是否可以在不使用 @EnableWebMvc 或 WebMvcConfigurationSupport 的情况下从外部资源获取图像?

谢谢

标签: spring-bootspring-mvcconfiguration

解决方案


好的,我解决了这个问题。首先我删除了@EnableWebMvc 和 WebMvcConfigurationSupport,然后我注意到 url 中存在问题。我放 :

.addResourceLocations("file:/opt/files/");

代替:

.addResourceLocations("file:///C:/opt/files/");

因为我是 Windows 用户。

谢谢


推荐阅读