spring - Spring boot 2:为每种语言映射 addResourceHandler 返回 404
问题描述
我正在尝试设置以根据 URL 中的语言参数提供静态内容,我得到404
了,以下代码中的映射有什么问题?
private PathResourceResolver getResolver() {
return new PathResourceResolver() {
@Override
protected Resource resolveResourceInternal(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
logger.info("REQ PATH :: " + requestPath + " " + locations);
return super.resolveResourceInternal(request, requestPath, locations, chain); //To change body of generated methods, choose Tools | Templates.
}
};
}
和静态文件夹结构:
404
以下是我尝试访问 URL 时显示的完整日志https://localhost:8443/fr
解决方案
我能够解决这个问题,如果其他人遇到类似的问题,下面的解决方案对我有用。
addResourceLocations
路径必须以/
否则该StringUtils
applyRelativePath
方法将删除提供的路径的最后一部分,在我的情况下en|fr|de..
/**
* Apply the given relative path to the given Java resource path,
* assuming standard Java folder separation (i.e. "/" separators).
* @param path the path to start from (usually a full file path)
* @param relativePath the relative path to apply
* (relative to the full file path above)
* @return the full file path that results from applying the relative path
*/
public static String applyRelativePath(String path, String relativePath) {
int separatorIndex = path.lastIndexOf(FOLDER_SEPARATOR);
if (separatorIndex != -1) {
String newPath = path.substring(0, separatorIndex);
if (!relativePath.startsWith(FOLDER_SEPARATOR)) {
newPath += FOLDER_SEPARATOR;
}
return newPath + relativePath;
}
else {
return relativePath;
}
}
推荐阅读
- c# - Creating Exe/MSI for C# Windows Forms using Visual Studio 2017 Setup Project
- ruby - 从 BasicObject 继承以提高性能
- java - 在 },{ 上拆分字符串
- javascript - onclick="window.open" + html?
- r - 为什么排序比 R 中的排序函数慢?
- javascript - 我的 f7/vue 单选按钮没有按预期工作
- c++ - 如何使用 std::make_unique() 在构造函数中启动 std::unique_ptr
- batch-file - 使用 BAT 文件时如何在 CMD 中输入命令?
- findersync - Finder Cover 流程分配第二个 FIFinderSync
- ruby-on-rails - 通过rails控制台将用户分配为rails中的管理员