欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-05 08:37:02
前言 spring boot项目一般都是内嵌tomcat或者jetty服务器运行,很少用war包部署到外部的服务容器,即使放到linux中,一般也是直接启动applica...

前言

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

总结

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