Preface
Spring Boot projects are usually run in embedded tomcat or jetty servers, and they rarely use war packages to deploy to external service containers. Even if placed in Linux, they usually start the Application class directly. However, sometimes we need to deploy to external servers, which is a bit troublesome for Spring Boot.
Below is a record of the problems encountered in Tomcat when you deployed the first SpringBoot project. Three things you need to pay attention to: headaches...
Details are as follows:
1. SpringBoot has its own built-in Tomcat container, so you should tell it not to use the built-in container and do not specify the Tomcat version configuration as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
2. The Application class needs to be changed as follows, where exclude is to ignore the annotation of the database (I didn't make the database):
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }3. The JDK version must be always! I started using jdk1.7 for the server and 1.8 for the local environment. As a result, it was normal for the local debugging and running. The server tomcat started normally, the jsp access was normal, all interfaces were 404, and the console did not have SpringBoot sign! ! ! ! Researched for a whole day... Headache...
My code:
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 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>wechatServer</artifactId> <packaging>war</packaging> <url>http://maven.apache.org</url> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- Exclude built-in containers, exclude built-in containers from exporting them into war packages to allow external containers to run spring-boot projects--> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional><!-- optional=true, the dependency will not be passed, the project depends on devtools; if the project that depends on the myboot project wants to use devtools, it needs to be reintroduced--> </dependency> </dependencies> <!--Modify the packaging settings--> <build> <plugins> <!-- <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <encoding>${project.build.sourceEncoding}</encoding> <skipTests>true</skipTests> <!-- Name when typing it into a war package --> <warName>wechatServer</warName> <!-- Main path enabled when mave (because the main function is also added when testing in other packages) --> <mainClass>${start-class}</mainClass> <skip>true</skip><!-- Skip test--> <testFailureIgnore>true</testFailureIgnore> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project> Application class:
package wechatService.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class}) public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } } Hello request:
@RestController @RequestMapping public class LoginController { @RequestMapping("/hello/{myName}") String index(@PathVariable String myName) { return "Hello "+myName+"!!!"; } }I only configure ports for application.properties, but after deployment to tomcat, it will not work here:
server.port = 80
My directory structure:
Finally my complete code: http://xiazai.VeVB.COM/201803/yuanma/wechatServer(VeVB.COM).rar
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.