feichangh
feichangh

Reputation: 1453

Setup spring-boot app on tomcat

I followed a simple guide and created a spring-boot based web application on my local, using embedded tomcat, it works fine. The next step I am trying to deploy it onto a remote linux tomcat server. I followed these steps first:

(1) Update application’s main class to extend SpringBootServletInitializer. SpringBootApplication is my previous main class

public class WebInitializer extends SpringBootServletInitializer {   
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringBootWebApplication.class);
}    
}

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

@Controller
public class IndexController {
    @RequestMapping("/")
    String index(){
        return "index";
    }
}

(2) Update build configuration so that project produces a war file rather than a jar file.

<packaging>war</packaging>

(3) Mark the embedded servlet container dependency as provided. I do not have this dependency before only the spring-boot-starter-web I just added one

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

(4) I also added app path in application.properties

server.context-path = /springbootdemo

After running maven-clean and maven-install I get my spring-boot-web-0.0.1-SNAPSHOT.war and then put it into tomcat webapp in remote server, but I am getting 404 on both of them:

http://host:port/springbootdemo
http://host:port/spring-boot-web
http://host:port/spring-boot-web/springbootdemo

What should be the right path here to my app? When running on local it is running on http://localhost:8080/ without a app name. When I see server catalina.out logs there no exception and server get startup.

Upvotes: 1

Views: 508

Answers (3)

user6089500
user6089500

Reputation:

Try http://localhost:8080/springbootdemo/spring-boot-web-0.0.1-SNAPSHOT please. It may work i guess.

Upvotes: 1

Andy Wilkinson
Andy Wilkinson

Reputation: 116031

server.context-path has no effect when you deploy a war file to an external container.

Tomcat uses the whole name of the war, minus the .war suffix, for an app's context path. Assuming that you copied spring-boot-web-0.0.1-SNAPSHOT.war to Tomcat's webapps directory, your app will be available at http://localhost:8080/spring-boot-web-0.0.1-SNAPSHOT.

Upvotes: 2

Marco Tedone
Marco Tedone

Reputation: 602

You can see my answer here.

The answer contains also a link to a demo project which you can build and deploy to Tomcat.

Upvotes: -1

Related Questions