Spring Boot部署到Tomcat过程中遇到的问题汇总

时间:2021-12-22 00:28:27

前言

Spring Boot项目一般都是内嵌tomcat或者jetty服务器运行,很少用war包部署到外部的服务容器,即使放到linux中,一般也是直接启动Application类,但是有些时候我们需要部署到外部的服务器,这对于Spring Boot来说却有点麻烦

下面就记录下自己部署的第一个SpringBoot项目到Tomcat中遇到的问题,三个地方需要注意:头疼……

详细如下:

1、SpringBoot有自己内置Tomcat容器,所以要告诉它不使用内置容器,不指定Tomcat版本配置如下:

?
1
2
3
4
5
<dependency>
<groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope>
 </dependency>

2、Application类需要如下改动,其中exclude是忽略数据库的注解(我没弄数据库):

?
1
2
3
4
5
6
7
8
9
10
11
12
@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、JDK版本要一直!我开始服务器用的jdk1.7,本地环境用的1.8,结果本地怎么调试运行都正常,服务器tomcat启动正常,jsp访问都正常,接口全部404,控制台也没有出现SpringBoot的标示!!!!研究了一整天……头疼……

我的代码:

pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<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>
  <!-- 排除内置容器,排除内置容器导出成war包可以让外部容器运行spring-boot项目 -->
 </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,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 -->
 </dependency>
 </dependencies>
 <!--修改打包设置 -->
 <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>
   <!-- 打成war包时名子 -->
   <warName>wechatServer</warName>
   <!-- mave时启用的main路径(因为其他包里面测试时也加了main函数) -->
   <mainClass>${start-class}</mainClass>
   <skip>true</skip><!-- 跳过测试 -->
<testFailureIgnore>true</testFailureIgnore>
<failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
  </plugin>
 </plugins>
 </build>
</project>

Application类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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请求:

?
1
2
3
4
5
6
7
8
@RestController
@RequestMapping
public class LoginController {
 @RequestMapping("/hello/{myName}")
 String index(@PathVariable String myName) {
 return "Hello "+myName+"!!!";
 }
}

application.properties我这里只配置了端口,然而部署到tomcat以后这里就不起作用了:

?
1
server.port = 80

我的目录结构:

最后我的完整代码:wechatServer.rar

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:http://blog.csdn.net/fanxiangru999/article/details/79207059