温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-06 09:13:50 来源:脚本之家 阅读:201 作者:Big丶Fine 栏目:编程语言

前言

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

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

详细如下:

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

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

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

@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

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

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请求:

@RestController 
@RequestMapping 
public class LoginController { 
 @RequestMapping("/hello/{myName}") 
 String index(@PathVariable String myName) { 
 return "Hello "+myName+"!!!"; 
 } 
} 

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

server.port = 80 

我的目录结构:

最后我的完整代码:http://xiazai.jb51.net/201803/yuanma/wechatServer(jb51.net).rar

总结

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI