概述
上篇记录了IDEA上创建 Maven SpringBoot+mybatisplus+thymeleaf 项目,但是如何将SpringBoot发布到Tomcat,直接采用Maven 命令Clear-Package指令之后,生成的War并不能访问,任然显示404错误。所以这个时候,需要修改代码内容部分;
代码调整项
1、修改pom.xml文件;
(1)、finalName要和Application.properties中的server.context-path值保持一致;
<build> <finalName>htcv</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins>
(2)、pom加上javax.servlet.servlet-api依赖项;
<dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency>
(3)、发布的时候排除内嵌tomcat服务器,避免冲突;
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
完整的pom.xml配置如下,记得将<packaging>jar</packaging>修改成<packaging>war</packaging>
<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> <groupId>com.justin</groupId> <artifactId>htcspringboot</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>Maven Quick Start Archetype</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <jdk.version>1.8</jdk.version> <mybatis-starter-version>1.2.0</mybatis-starter-version> <mybatis.plus.version>2.1.0</mybatis.plus.version> <ojdbc14.version>10.2.0.5.0</ojdbc14.version> <druid.version>1.0.28</druid.version> <spring.boot.version>1.5.2.RELEASE</spring.boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> <version>${spring.boot.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis-starter-version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybatis.plus.version}</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>${ojdbc14.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> </dependencies> <build> <finalName>htcv</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project>
2、修改java文件内;
让程序入口Main方法类继承SpringBootServletInitializer,重写configure方法;
import com.justin.model.Einvoice; import com.justin.serviceapi.EinvoiceService; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.Banner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @MapperScan("com.justin.dao") public class Application extends SpringBootServletInitializer { @Autowired private EinvoiceService einvoiceservice; @RequestMapping("/") public String Greeting() { return "Justin Say Hello WOrld!"; } @RequestMapping("/Add") public String AddEinvoice() { Einvoice Einv=new Einvoice(); Einv.setInvoicecode("98234728"); Einv.setInvoicenum("1111111"); einvoiceservice.insertOne(Einv); return "INser SUCC"; } @RequestMapping("/check/{invoicecode}") public String ShowInfo(@PathVariable("invoicecode") String invoicecode) { return einvoiceservice.GetInvoiceNUmByCode(invoicecode); } @RequestMapping("/Update/{invoicecode}/{invoicenum}") public String UpInfo(@PathVariable("invoicecode") String invoicecode,@PathVariable("invoicenum") String invoicenum) { einvoiceservice.ChangeCode(invoicenum,invoicecode); return "SUCC"; } public static void main(String[] args) { SpringApplication.run(Application.class,args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return configureApplication(builder); } private static SpringApplicationBuilder configureApplication(SpringApplicationBuilder builder) { return builder.sources(Application.class).bannerMode(Banner.Mode.OFF); } }
重新打包发布即可,输入网址 http://localhost:8080/htcv//HE/CIndex 即可访问自定义的页面内容项;
参考博客网址
spring boot项目发布tomcat容器(包含发布到tomcat6的方法) http://www.cnblogs.com/weixliu/p/6432342.html
来源:https://www.cnblogs.com/xibei666/p/7794266.html