Spring Boot War 部署到 Tomcat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27904594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot War deployed to Tomcat
提问by Daniil Shevelev
I am trying to deploy a Spring Boot app to Tomcat, because I want to deploy to AWS. I created a WAR file, but it does not seem to run on Tomcat, even though it is visible.
我正在尝试将 Spring Boot 应用程序部署到 Tomcat,因为我想部署到 AWS。我创建了一个 WAR 文件,但它似乎不能在 Tomcat 上运行,即使它是可见的。
Details:
0. Here is my app:
详细信息:
0。这是我的应用程序:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class App {
public static void main(String[] args) {
SpringApplication.run(SampleController.class, args);
}
}
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/help")
@ResponseBody
String home() {
String input = "Hi! Please use 'tag','check' and 'close' resources.";
return input;
}
}
application.properties has following:
application.properties 具有以下内容:
server.port=${port:7777}
After reading a number of pagesand question-answersI added following to my POM:
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.niewlabs</groupId> <artifactId>highlighter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies>
I ran"mvn package"and got WAR file (size 250Mb), which I put into "webapps" folder.
- I started Tomcat and am able to see my app listed, in my case "/highlighter-1.0-SNAPSHOT".
- Clicking on the link for the app results in "Status 404" page.
- When I run Spring Boot app just by itself, without container it runs on localhost:7777, but there is nothing there when I run it in Tomcat.
http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0
<groupId>com.niewlabs</groupId> <artifactId>highlighter</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.1.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> </dependencies>
我运行了“mvn package”并获得了 WAR 文件(大小为 250Mb),我将其放入“webapps”文件夹中。
- 我启动了 Tomcat 并且能够看到我的应用程序被列出,在我的例子中是“/highlighter-1.0-SNAPSHOT”。
- 单击该应用程序的链接会显示“状态 404”页面。
- 当我单独运行 Spring Boot 应用程序时,它在没有容器的情况下在 localhost:7777 上运行,但是当我在 Tomcat 中运行它时没有任何内容。
Update: There is another reference. Not sure how useful it is.
更新:还有另一个参考。不确定它有多大用处。
回答by Daniil Shevelev
This guide explains in detail how to deploy Spring Boot app on Tomcat:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
本指南详细解释了如何在 Tomcat 上部署 Spring Boot 应用程序:http:
//docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
Essentially I needed to add following class:
基本上我需要添加以下类:
public class WebInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(App.class);
}
}
Also I added following property to POM:
我还向 POM 添加了以下属性:
<properties>
<start-class>mypackage.App</start-class>
</properties>
回答by Cesar Chavez
Hey make sure to do this changes to the pom.xml
嘿,请确保对 pom.xml 进行此更改
<packaging>war</packaging>
in the dependencies section make sure to indicated the tomcat is provided so you dont need the embeded tomcat plugin.
在依赖项部分确保指明提供了 tomcat,因此您不需要嵌入的 tomcat 插件。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
This is the whole pom.xml
这是整个 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<start-class>com.example.Application</start-class>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
And the Application class should be like this
而Application类应该是这样的
Application.java
应用程序.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
/**
* Used when run as JAR
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
/**
* Used when run as WAR
*/
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
And you can add a controller for testing MyController.java
你可以添加一个控制器来测试 MyController.java
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
@RequestMapping("/hi")
public @ResponseBody String hiThere(){
return "hello world!";
}
}
Then you can run the project in a tomcat 8 version and access the controller like this
然后就可以在tomcat 8版本下运行项目,像这样访问控制器
If for some reason you are not able to add the project to tomcat do a right click in the project and then go to the Build Path->configure build path->Project Faces
如果由于某种原因您无法将项目添加到 tomcat,请在项目中右键单击,然后转到 Build Path->configure build path->Project Faces
make sure only this 3 are selected
确保只选择这 3 个
Dynamic web Module 3.1 Java 1.8 Javascript 1.0
动态网页模块 3.1 Java 1.8 Javascript 1.0
回答by Software Engineer
I think you're confused by different paradigms here. First, war files and server deployment -- those things belong to Java Enterprise Edition (Java EE). These concepts have no real place in a spring-boot application, which follows a different model.
我认为您对这里的不同范式感到困惑。首先,war 文件和服务器部署——这些东西属于 Java Enterprise Edition (Java EE)。这些概念在遵循不同模型的 spring-boot 应用程序中没有真正的位置。
Spring-boot is responsible for creating an embedded container and running your services within it directly from standard jar files (although it can do a lot more). I think the intent of this model is to support micro-service development -- where each service has its own container and is completely self contained. You can use your code to generate Java EE apps too, but that would be silly considering that spring-boot is a lot easier (for certain types of application/service).
Spring-boot 负责创建一个嵌入式容器并直接从标准 jar 文件在其中运行您的服务(尽管它可以做更多的事情)。我认为这个模型的目的是支持微服务开发——每个服务都有自己的容器,并且是完全自包含的。您也可以使用您的代码生成 Java EE 应用程序,但考虑到 spring-boot 更容易(对于某些类型的应用程序/服务),这将是愚蠢的。
So, given this information you now have to decide what paradigm you're going to follow, and you need to follow that and only that.
因此,鉴于这些信息,您现在必须决定要遵循的范式,并且您只需要遵循那个范式。
Spring-boot is executable -- you just have to run the main method in the App class which you can do from the command line or using your favourite IDE or maven or gradle (tip: maven is the right answer). This will bring up a tomcat server (by default) and your service will be available within it. Given the configuration you posted above your service should be available at: http://localhost:7777/context/help
-- the context
is meant to be replaced with your context name, which you haven't shared.
Spring-boot 是可执行的——你只需要在 App 类中运行 main 方法,你可以从命令行或使用你最喜欢的 IDE 或 maven 或 gradle(提示:maven 是正确的答案)。这将启动一个 tomcat 服务器(默认情况下)并且您的服务将在其中可用。鉴于您在上面发布的配置,您的服务应该在以下位置可用:http://localhost:7777/context/help
--context
将替换为您尚未共享的上下文名称。
You aren't meant to be creating a war, running tomcat, or deploying anything. None of that is necessary in spring-boot. The packaging in your pom should be jar
, not war
and the scope
of the spring-boot-starter-tomcat
should be removed -- it certainly isn't provided.
您不打算制造War、运行 tomcat 或部署任何东西。在 spring-boot 中这些都不是必需的。你的 pom 中的包装应该是jar
, notwar
并且scope
的spring-boot-starter-tomcat
应该被移除——它当然没有提供。
When you run your main method, the console output should tell you the context that you've registered; use that to get the URL right.
当你运行你的 main 方法时,控制台输出应该告诉你你注册的上下文;使用它来获取正确的 URL。
Having said all that, spring-boot has to exist in a JEE world for now (until it is widely adopted). For that reason, the spring people have documented an approach to building a war rather than an executable jar, for deployment to a servlet or JEE container. This allows a lot of the spring-boot tech to be used in environments where there are restrictions against using anything but wars (or ears). However, this is a just a response to the fact that such environments are quite common, and is not seen as a necessary, or even desirable, part of the solution.
尽管如此,spring-boot 目前必须存在于 JEE 世界中(直到它被广泛采用)。出于这个原因,spring 人员记录了一种构建War而不是可执行 jar 的方法,用于部署到 servlet 或 JEE 容器。这使得许多 spring-boot 技术可以在除了War(或耳朵)之外的任何东西都受到限制的环境中使用。然而,这只是对这样一个事实的回应,即此类环境非常普遍,并且不被视为解决方案的必要部分,甚至是不可取的部分。
回答by Jaishant Biradar
Your Application.java
class should extend the SpringBootServletInitializer
class
ex:
你的Application.java
类应该扩展SpringBootServletInitializer
类例如:
public class Application extends SpringBootServletInitializer {}
回答by Marcin Szymczak
Solution for people using Gradle
使用 Gradle 的人的解决方案
Add plugin to build.gradle
添加插件到 build.gradle
apply plugin: 'war'
Add provideddependency to tomcat
将提供的依赖项添加到 tomcat
dependencies {
// other dependencies
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
回答by EpicPandaForce
After following the guide (or using Spring Initializr), I had a WAR that worked on my local computer, but didn't work remote (running on Tomcat).
按照指南(或使用 Spring Initializr)之后,我有一个 WAR 可以在我的本地计算机上运行,但不能远程运行(在 Tomcat 上运行)。
There was no error message, it just said "Spring servlet initializer was found", but didn't do anything at all.
没有错误消息,它只是说“找到了 Spring servlet 初始化程序”,但根本没有做任何事情。
17-Aug-2016 16:58:13.552 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.4
17-Aug-2016 16:58:13.593 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /opt/tomcat/webapps/ROOT.war
17-Aug-2016 16:58:16.243 INFO [localhost-startStop-1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
and
和
17-Aug-2016 16:58:16.301 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log 2 Spring WebApplicationInitializers detected on classpath
17-Aug-2016 16:58:21.471 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing Spring embedded WebApplicationContext
17-Aug-2016 16:58:25.133 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
17-Aug-2016 16:58:25.133 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
Nothing else happened. Spring Boot just didn't run.
没有其他事情发生。Spring Boot 只是没有运行。
Apparently I compiled the server with Java 1.8, and the remote computer had Java 1.7.
显然我用 Java 1.8 编译了服务器,而远程计算机有 Java 1.7。
After compiling with Java 1.7, it started working.
用 Java 1.7 编译后,它开始工作了。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version> <!-- added this line -->
<start-class>myapp.SpringApplication</start-class>
</properties>
回答by junnyea
public class Application extends SpringBootServletInitializer {}
公共类应用程序扩展 SpringBootServletInitializer {}
just extends the SpringBootServletInitializer. It will works in your AWS/tomcat
只是扩展了 SpringBootServletInitializer。它将适用于您的 AWS/tomcat
回答by VISHWANATH N P
回答by Vojtech Ruzicka
If you are creating a new app instead of converting an existing one, the easiest way to create WAR based spring boot application is through Spring Initializr.
如果您要创建新应用程序而不是转换现有应用程序,创建基于 WAR 的 Spring Boot 应用程序的最简单方法是通过Spring Initializr。
It auto-generates the application for you. By default it creates Jar, but in the advanced options, you can select to create WAR. This war can be also executed directly.
它会自动为您生成应用程序。默认情况下它会创建 Jar,但在高级选项中,您可以选择创建 WAR。这一战也可以直接执行。
Even easier is to create the project from IntelliJ IDEA directly:
更简单的是直接从 IntelliJ IDEA 创建项目:
File → New Project → Spring Initializr
文件 → 新建项目 → Spring Initializr
回答by Surasin Tancharoen
Update 2018-02-03 with Spring Boot 1.5.8.RELEASE.
使用 Spring Boot 1.5.8.RELEASE 更新 2018-02-03。
In pom.xml, you need to tell Spring plugin when it is building that it is a war file by change package to war, like this:
在 pom.xml 中,您需要在构建时通过将包更改为 war 来告诉 Spring 插件它是一个 war 文件,如下所示:
<packaging>war</packaging>
Also, you have to excluded the embedded tomcat while building the package by adding this:
此外,您必须在构建包时通过添加以下内容来排除嵌入式 tomcat:
<!-- to deploy as a war in tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
The full runable example is in here https://www.surasint.com/spring-boot-create-war-for-tomcat/
完整的可运行示例在这里 https://www.surasint.com/spring-boot-create-war-for-tomcat/