【问题标题】:Tomcat Deployment of Spring Boot is giving 404Spring Boot的Tomcat部署给出404
【发布时间】:2021-05-14 15:03:01
【问题描述】:

我正在尝试在 Tomcat 8 上部署 Spring Boot Rest Controller。当我直接从 Eclipse 运行它时,它工作正常。但是在将其部署到 Tomcat 8 时,出现 404 错误。

我已按照这些说明 (https://www.baeldung.com/spring-boot-war-tomcat-deploy) 进行了四处搜索,但似乎没有任何效果。

这是我的控制器:

import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TomcatController {

    @GetMapping("/hello")
    public Collection<String> sayHello() {
        return IntStream.range(0, 10)
          .mapToObj(i -> "Hello number " + i)
          .collect(Collectors.toList());
    }
    
}

这是我的申请:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;


@SpringBootApplication
public class SsoApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(SsoApplication.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SsoApplication.class);
    }
}

这是我的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.test</groupId>
  <artifactId>sso</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>SSOServices</name>
  <description>SSO Services</description>
  <packaging>war</packaging>
  
    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId> 
      <version>2.4.0</version> 
      <relativePath/> 
    </parent> 
    <dependencies>
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>${artifactId}</finalName>
    </build>
    
</project>

任何帮助将不胜感激。 总而言之,当我使用“localhost:8080/hello”通过 eclipse 进行测试时,它可以工作。 当我部署到 tomcat 8 并使用“localhost:8080/sso/hello”进行测试时,我得到了 404。

【问题讨论】:

  • 我很久没用tomcat了,不过请检查一下tomcat的配置。
  • Tomcat 工作正常。如果我将 test.txt 文件直接放在“webapps/sso”下,我可以使用 URL localhost:8080/sso/test.txt 加载它。
  • 要让 Spring Boot 2.4 正常工作,您至少需要使用 Tomcat 8.5(不仅仅是 8)甚至更好的 tomcat 9。但是,为什么还要麻烦部署而不是仅仅运行 war(或更好)jar使用嵌入式容器?
  • 就是这样。谢谢。要回答您的问题,我知道作为容器运行会更容易,但我的客户端已经有一个 Tomcat 实例,可以完全按照他们的要求进行设置,并希望将所有应用程序放在一起。再次感谢您的帮助。

标签: java spring spring-boot maven tomcat


【解决方案1】:

来自 M. Deinum(上图)

要使 Spring Boot 2.4 正常工作,您至少需要使用 Tomcat 8.5(不仅仅是 8)甚至更好的 Tomcat 9。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2014-09-04
    • 2015-03-10
    • 2018-12-05
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多