Tomcat Deployment of Spring Boot is giving 404

Viewed 103

I'm trying to deploy a Spring Boot Rest Controller on Tomcat 8. When I run it directly from Eclipse, it works fine. But when deploying it to Tomcat 8, I get a 404 error.

I've followed these instructions (https://www.baeldung.com/spring-boot-war-tomcat-deploy) and searched everywhere, but nothing seems to work.

Here is my Controller:

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());
    }
    
}

Here is my Application:

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);
    }
}

And here is my 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>

Any help would be greatly appreciated. To summarize, when I test through eclipse with "localhost:8080/hello", it works. When I deploy to tomcat 8 and test with "localhost:8080/sso/hello", I get a 404.

1 Answers

From M. Deinum (above)

For Spring Boot 2.4 to work you would need to use Tomcat 8.5 as minimum (not just 8) or even better tomcat 9.

Related