Home > Software design >  Can't deploy war to Tomcat
Can't deploy war to Tomcat

Time:01-22

I have spring boot application and it works when i run it with spring boot,but when I build war and deploy it to tomcat,i get 404 status

I added this lines from spring docs to maven:

    <properties>

        <start-class>com.example.deploytest.DeploytestApplication</start-class>
    </properties>

    <packaging>war</packaging>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
    </dependency>

And here is my main class:

@SpringBootApplication
public class DeploytestApplication extends SpringBootServletInitializer {

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

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(DeploytestApplication.class);
    }
}

And simple controller:

@RestController
public class MainController {

    @GetMapping
    public String hello() {
        return "hello world";
    }
}

My tomcat version is 10.0.27

What is wrong here?

CodePudding user response:

The pom.xml should look similar to this:

<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>

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

If missing you can also try to add the SpringBootServletInitializer interface in you main class:

@SpringBootApplication
public class SpringBootTomcatApplication extends SpringBootServletInitializer {
}

CodePudding user response:

try to give an url to your GetMapping

@GetMapping("/")
  • Related