启动时debug
org.springframework.boot.web.servlet.server.DocumentRoot
public final File getValidDirectory() {
File file = this.directory;
file = (file != null) ? file : getWarFileDocumentRoot();
file = (file != null) ? file : getExplodedWarFileDocumentRoot();
file = (file != null) ? file : getCommonDocumentRoot();
if (file == null && this.logger.isDebugEnabled()) {
logNoDocumentRoots();
}
else if (this.logger.isDebugEnabled()) {
this.logger.debug("Document root: " + file);
}
return file;
}
这段代码
private File getCommonDocumentRoot() {
for (String commonDocRoot : COMMON_DOC_ROOTS) {
File root = new File(commonDocRoot);
if (root.exists() && root.isDirectory()) {
return root.getAbsoluteFile();
}
}
return null;
}
其实是想找到 user.dir默认项目根路径下是否存在如下这几个文件夹,存在则返回根路径,这样就可以映射到具体文件夹下面。
private static final String[] COMMON_DOC_ROOTS = { "src/main/webapp", "public",
"static" };
但是如果多层级,此时打印System.getProperty(“user.dir”) 为project的根目录,当找不到这些文件夹,该方法返回null,映射失败才会导致404。
如图设置后 再次打印 System.getProperty(“user.dir”) 会发现此时路径为module的路径,这样就可以找到以上文件夹,正确映射后 就不会404了。