如何修改Spring Boot2嵌入式tomcat的文档根目录

时间:2019-06-22 02:09:05

标签: java spring-boot tomcat

如何修改内置tomcat的文档根目录,而不是使用“ src / main / webapp”

2 个答案:

答案 0 :(得分:0)

在application.properties中,添加一个属性:

server.servlet.contextPath=/yourpathgoeshere

答案 1 :(得分:0)

server.tomcat.document-root中添加application.properties

@Value("${server.tomcat.document-root:}")
private String documentRoot;
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
        @Override
        public void customize(ConfigurableWebServerFactory factory) {
            if (factory instanceof TomcatServletWebServerFactory) {
                TomcatServletWebServerFactory tomcat = (TomcatServletWebServerFactory) factory;
                if (!StringUtils.isEmpty(documentRoot)) {
                    File root = new File(documentRoot);
                    tomcat.setDocumentRoot(root);
                }
            }
        }
    };
}