The simplest way to host a Spring Boot application is to serve it from the Tomcat application server. Tomcat is a server that can be installed on all major operating systems.
The main advantage that Tomcat provides is the ability to serve multiple applications at the same time. This is essential for many organizations, because most organizations will eventually want the ability to run multiple different back end applications at the same time.
The instructions below assume that you have already installed Tomcat on your computer.
To run your application in Tomcat you will still need to set up MySQL on the same computer and install the database for your application.
The process of deploying a Spring Boot application to Tomcat is pretty straightforward. The first step is to make a small modification to the application class. We replace the existing content of the file that has our application class with the following:
package edu.lawrence.auction;
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 JpaauctionApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(JpaauctionApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(JpaauctionApplication.class, args);
}
}
Assuming that you have installed the database into MySQL you can also remove the line
spring.jpa.hibernate.ddl-auto = update
from your application.properties file.
Also, if you application is complete and ready to deploy you can safely remove any test classes from the application.
Next, we make a small change to how our application gets built. By default, Spring Boot applications are packaged as Java jar files. To deploy our application to Tomcat we are going to switch to using a web archive, or war file. To do this we replace the line
<packaging>jar</packaging>
in our pom.xlm file with
<packaging>war</packaging>
We then build the war file by running the command
mvn install
in the terminal. This will result in the war file being built and placed in the project's target folder.
Since the default name for the war file will be fairly long and complex, we will rename it to something much simpler, auction.war.
To deploy our application to Tomcat we simply have to drag a copy of the war file into the Tomcat server's webapps folder. On a Linux system that folder is typically located at /opt/tomcat/webapps.
Once the war file is installed you can reach the application at the base URL
http://localhost:8080/auction