【问题标题】:Spring Boot App works on local Tomcat but not on remote serverSpring Boot App 适用于本地 Tomcat,但不适用于远程服务器
【发布时间】:2015-11-14 05:27:00
【问题描述】:

这个项目有很多变化的部分,但我会尽量简短和客观。

基本上,我有一个 Spring Boot 应用程序,它可以在带有 Tomcat 7.0.63 的 Netbeans 上编译和运行良好。当我将 Netbeans 上生成的 .war 文件直接拖放到 Tomcat 目录上的 webapps/ 文件夹时,我也可以正常运行。

但是,当我尝试将相同的 .war 文件部署到在远程服务器上运行的 Tomcat 7.0.63 时,我得到的只是 Tomcat 404 错误页面。

Catalina.out 上没有产生错误并且应用部署成功,但它似乎只是指向根应用程序文件夹而不是加载 Spring DispatcherServlet。我之所以这么说是因为如果我将示例index.html 添加到根文件夹,它将呈现而不是 404 错误。

这是我的SpringBootServletInitializer

@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class RecruitingDashboardApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(RecruitingDashboardApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(RecruitingDashboardApplication.class, args);
   }


}

这是我的WebMvcConfigurerAdapter

@EnableAutoConfiguration
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Bean 
    public ViewResolver viewResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setSuffix(".html");

        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver);

        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(engine);
        return viewResolver;
    }


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("dashboard");
        registry.addViewController("/login").setViewName("login");
    }

}

还值得注意的是,我没有web.xml。一切要么由 Spring Boot 自动配置,要么通过上述文件配置 Java。

最后,因为我知道这会出现在 cmets 上,所以这是我的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.mycompany</groupId>
<artifactId>RecruitingDashboard</artifactId>
<packaging>war</packaging>

<name>Recruiting Dashboard</name>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <tomcat.version>7.0.63</tomcat.version>
    <start-class>recruitingdashboard.RecruitingDashboardApplication</start-class>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
        <version>1.9.22</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

【问题讨论】:

  • 我不确定这是否是您的问题的原因,但是as described here 您需要将嵌入式 Tomcat 依赖项标记为已提供,以阻止它干扰您部署到的 servlet 容器
  • 感谢您的评论。我已尝试将其标记为已提供,并且还尝试排除此 this answer 中描述的 tomcat 依赖项。两者仍然产生相同的结果。 Catalina.out 根本没有任何启动 spring boot 的迹象。
  • 你得到答案了吗?

标签: java spring maven tomcat spring-boot


【解决方案1】:

可能有点晚了,但其他人可能会像我一样偶然发现这个问题。

在本地 tomcat 上部署对我来说效果很好,但是在 ubuntu 上的远程 tomcat 上部署时,我也得到了 Tomcat 404 错误页面。

我也尝试过部署

<java.version>1.8</java.version>

但没有 tomcat 版本(我使用的是 Apache Tomcat/7.0.52。作为参考)。

使用属性部署后:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>controllers.Application</start-class>
</properties>

它按原样工作。 部署与

<java.version>1.7</java.version>

同样有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 2011-02-03
    • 2012-09-09
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多