spring-boot - @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,我也会遇到同样的问题。
为什么我会遇到这个问题?
是否可以在不使用 @EnableWebMvc 或 WebMvcConfigurationSupport 的情况下从外部资源获取图像?
谢谢
解决方案
好的,我解决了这个问题。首先我删除了@EnableWebMvc 和 WebMvcConfigurationSupport,然后我注意到 url 中存在问题。我放 :
.addResourceLocations("file:/opt/files/");
代替:
.addResourceLocations("file:///C:/opt/files/");
因为我是 Windows 用户。
谢谢
推荐阅读
- python-3.x - 为什么我不能在与块相同的情况下循环两次 CSV 行?
- azure - 与 GetMetadata 存在总是返回 true
- interface - Refer to super in default values of constructor parameters when implementing an interface
- kubernetes - 在 Kubernetes 中从容器写入主机
- php - Replacing array_key_exists() with isset() in PHP
- node.js - 正则表达式在 mongodb 命令行中工作,但不适用于 mongoose
- angularjs - 量角器在父元素中查找元素
- android - 如何在 android ListView 的分页滚动中滚动到最后一页正常工作
- powerbi - Power Query to load Web data but no table shows
- amazon-web-services - Amazon EC2 上的 thingsboard 不显示数据