eclipse maven项目 class类部署不到tomcat下_Springboot (五) Springboot之Tomcat部署

072452cbf98a272d130b73a1a15fc325.png

通过使用Spring Boot应用程序,可以创建一个war文件以部署到Web服务器中。在本章中,将学习如何创建WAR文件并在Tomcat Web服务器中部署Spring Boot应用程序。

Spring Boot Servlet初始化程序

传统的部署方式是使Spring Boot应用程序@SpringBootApplication类扩展SpringBootServletInitializer类。 SpringBootServletInitializer类文件允许在使用Servlet容器启动时配置应用程序。

下面给出了用于JAR文件部署的Spring Boot应用程序类文件的代码 -

package com.felix.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

需要扩展类SpringBootServletInitializer以支持WAR文件部署。 Spring Boot应用程序类文件的代码如下 -

package com.felix.demo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.support.SpringBootServletInitializer;@SpringBootApplicationpublic class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}

设置Main类

在Spring Boot中,需要在构建文件中指定启动的主类。

对于Maven,在pom.xml属性中添加start类,如下所示 -

com.felix.demo.DemoApplication

对于Gradle,在build.gradle中添加主类名,如下所示 -

mainClassName="com.felix.demo.DemoApplication"

将打包JAR更新为WAR

使用以下代码将包装JAR更新为WAR。

对于Maven,在pom.xml 中将包装添加为WAR,如下所示 -

warXML

对于Gradle,在build.gradle 中添加应用程序插件和war插件,如下所示 -

apply plugin: 'war'apply plugin: 'application'

对于GradlNow,编写一个简单的Rest端点来返回字符串:"Hello World from Tomcat"。 要编写Rest端点,需要将Spring Boot Web starter依赖项添加到构建文件中。

对于Maven,使用如下所示的代码在pom.xml 中添加Spring Boot启动程序依赖项 -

org.springframework.boot spring-boot-starter-web

对于Gradle,使用如下所示的代码在build.gradle 中添加Spring Boot starter依赖项 -

dependencies { compile('org.springframework.boot:spring-boot-starter-web')}

现在,使用如下所示的代码在Spring Boot Application类文件中编写一个简单的Rest端点 -

package com.felix.demo;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.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping(value = "/") public String hello() { return "Hello World from Tomcat"; }}

打包应用程序

现在,使用Maven和Gradle命令创建一个WAR文件以部署到Tomcat服务器中,以打包应用程序,如下所示。

对于Maven,使用命令mvn package打包应用程序。 然后创建WAR文件,可以在目标目录中找到它,如下面给出的屏幕截图所示 -

bc4f07f1c37ec9ad8b11bf0f774e42c2.png

对于Gradle,使用命令gradle clean build打包应用程序。 然后,将创建WAR文件,可以在build/libs目录下找到它。观察

7e86a15a903991a27cea69e9b2d19a2a.png

此处给出的屏幕截图以便更好地理解 -

部署到Tomcat

现在,运行Tomcat服务器,并在webapps目录下部署WAR文件。观察此处显示的屏幕截图以便更好地理解 -

f8159c428c9acad14b1e9a95bb573a46.png

成功部署后,点击网页浏览器中的URL => http://localhost:8080/demo-0.0.1-SNAPSHOT/,观察输出结果如下图所示 -

4d95ee563f220135fb3c711590530dbb.png

完整代码如下:

文件:pom.xml -

<?xml version = "1.0" encoding = "UTF-8"?>4.0.0com.felix demo 0.0.1-SNAPSHOTwardemoDemo project for Spring Bootorg.springframework.boot spring-boot-starter-parent 1.5.8.RELEASEUTF-8UTF-81.8com.felix.demo.DemoApplicationorg.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test testorg.springframework.boot spring-boot-maven-plugin 

文件:build.gradle

buildscript { ext { springBootVersion = '1.5.8.RELEASE' } repositories { mavenCentral() }dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'apply plugin: 'war'apply plugin: 'application'group = 'com.felix'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8mainClassName = "com.felix.demo.DemoApplication"repositories { mavenCentral()}dependencies { compile('org.springframework.boot:spring-boot-starter-web') testCompile('org.springframework.boot:spring-boot-starter-test')}

Spring Boot应用程序类文件的代码如下 -

package com.felix.demo;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.RequestMapping;import org.springframework.web.bind.annotation.RestController;@SpringBootApplication@RestControllerpublic class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping(value = "/") public String hello() { return "Hello World from Tomcat"; }}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值