Java 将 Spring Boot 部署到 Tomcat
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24741816/
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
Deploy Spring Boot to Tomcat
提问by philthomas26
So I am new to Spring - so I thought I would try Spring Boot
所以我是 Spring 的新手 - 所以我想我会尝试 Spring Boot
I am using a Maven to build - I set up a view requests, when I run it as "App" looks like it starts tomcat up itself and i have a service at default 8080 port, so when I call localhost:8080/service
it's all good.
我正在使用 Maven 来构建 - 我设置了一个视图请求,当我将它作为“应用程序”运行时,它看起来像它自己启动 tomcat 并且我在默认的 8080 端口有一个服务,所以当我调用localhost:8080/service
它时一切都很好。
However when i try to package it up as a WAR to deploy to a server it doesn't work
但是,当我尝试将其打包为 WAR 以部署到服务器时,它不起作用
So i thought i would go back to basics and deploy it onto my tomcat server locally and see what happens
所以我想我会回到基础并将它部署到我本地的 tomcat 服务器上,看看会发生什么
So first off nothing happened - 404 - did a bit of googling at found out i need to include a web entry point like this - here's my main class
所以首先什么也没发生 - 404 - 做了一些谷歌搜索,发现我需要包含一个这样的网络入口点 - 这是我的主要课程
@ComponentScan
@EnableAutoConfiguration
public class App extends SpringBootServletInitializer
{
public static void main( String[] args ) {
SpringApplication.run(App.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(appClass);
}
private static Class<App> appClass = App.class;
}
so it looked like it worked, in the console i could see the red catalina startup text, then i saw the Spring boot start up (which i wasn't seeing before)
所以看起来它起作用了,在控制台中我可以看到红色的 catalina 启动文本,然后我看到了 Spring boot 启动(我以前没有看到过)
however when i try and call the service localhost:8080/service
i still get a 404
但是,当我尝试致电该服务时,localhost:8080/service
我仍然收到 404
when i run it as "App" it works a treat
当我将它作为“应用程序”运行时,它会很好用
is there something obvious i need to be doing ? to create the war i just
有什么明显的我需要做的吗?制造War,我只是
- changed the pom xml package type to war
- included the override in the code snippet above
- exported as a war
- 将 pom xml 包类型更改为 war
- 在上面的代码片段中包含覆盖
- 作为War出口
i have unzipped the WAR and it looked fine - there is a WEB-INF and a set of dependencies, etc.
我已经解压缩了 WAR,它看起来很好 - 有一个 WEB-INF 和一组依赖项等。
any help would be good - as anyone had similar problems when deploying spring boot apps as a WAR?
任何帮助都会很好 - 因为在将 Spring Boot 应用程序部署为 WAR 时有人遇到过类似的问题吗?
***UPDATE****
***更新****
Now i can get my war to deploy correctly on my local Tomcat 7 server - but when i try and deploy to an externally hosted Tomcat container i get
现在我可以让我的War在我的本地 Tomcat 7 服务器上正确部署 - 但是当我尝试部署到外部托管的 Tomcat 容器时,我得到
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 15 more
Jul 14, 2014 1:49:01 PM org.apache.catalina.startup.HostConfig deployDirectory
SEVERE: Error deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/cookpot
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/cookpot]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:898)
at org.apache.catalina.core.ContainerBase.access<?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">
<!-- ... -->
<packaging>war</packaging>
<!-- ... -->
<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>
</project>
0(ContainerBase.java:130)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:153)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:142)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:869)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1617)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Jul 14, 2014 1:49:01 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/examples
Jul 14, 2014 1:49:01 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/host-manager
Jul 14, 2014 1:49:01 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/CookPot
Jul 14, 2014 1:49:02 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/local/shared/tomcat/thecookpot/webapps/CookPot/WEB-INF/lib/tomcat-embed-core-7.0.52.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Jul 14, 2014 1:49:02 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/local/shared/tomcat/thecookpot/webapps/CookPot/WEB-INF/lib/tomcat-embed-el-7.0.52.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
Jul 14, 2014 1:49:06 PM org.apache.catalina.core.ContainerBase addChildInternal
SEVERE: ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/CookPot]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:895)
at org.apache.catalina.core.ContainerBase.accessSpring Boot makes it easy to create stand-alone [...]
Applications that can you can "just run".
Embed Tomcat or Jetty directly (no need to deploy WAR files)
0(ContainerBase.java:130)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:153)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:142)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:869)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1617)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.security.AccessControlException: access denied (java.util.PropertyPermission java.awt.headless write)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.System.setProperty(System.java:727)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:263)
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:130)
at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:89)
at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:51)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 15 more
Jul 14, 2014 1:49:06 PM org.apache.catalina.startup.HostConfig deployDirectory
SEVERE: Error deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/CookPot
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/CookPot]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:898)
at org.apache.catalina.core.ContainerBase.access@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan
public class ManufacturingRegionApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
new SpringApplicationBuilder(ManufacturingRegionApplication.class).application().run(args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder applicationBuilder) {
return applicationBuilder.sources(application);
}
private static Class<ManufacturingRegionApplication> application = ManufacturingRegionApplication.class;
0(ContainerBase.java:130)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:153)
at org.apache.catalina.core.ContainerBase$PrivilegedAddChild.run(ContainerBase.java:142)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:869)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:615)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1095)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1617)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Jul 14, 2014 1:49:06 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/ROOT
Jul 14, 2014 1:49:06 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/manager
Jul 14, 2014 1:49:06 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/docs
Jul 14, 2014 1:49:06 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/shared/tomcat/thecookpot/webapps/Cookpot
Jul 14, 2014 1:49:06 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/local/shared/tomcat/thecookpot/webapps/Cookpot/WEB-INF/lib/tomcat-embed-core-7.0.52.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
Jul 14, 2014 1:49:06 PM org.apache.catalina.loader.WebappClassLoader validateJarFile
INFO: validateJarFile(/usr/local/shared/tomcat/thecookpot/webapps/Cookpot/WEB-INF/lib/tomcat-embed-el-7.0.52.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/el/Expression.class
Jul 14, 2014 1:49:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-6350"]
Jul 14, 2014 1:49:09 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-4350"]
Jul 14, 2014 1:49:09 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 17529 ms
采纳答案by James
The chapter Packaging executable jar and war filesin the Spring Boot reference documentation states:
Spring Boot 参考文档中的Packaging executable jar and war files一章指出:
To build a war file that is both executable and deployable into an external container you need to mark the embedded container dependencies as “provided”, e.g:
要构建一个既可执行又可部署到外部容器中的 war 文件,您需要将嵌入式容器依赖项标记为“已提供”,例如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
回答by mcoolive
When we choose Spring Boot, we don't want to produce a WAR. From the root page:
当我们选择 Spring Boot 时,我们不想产生 WAR。从根页面:
@RestController
@RequestMapping(value = "/manufacturing-region-service")
public class ManufacturingRegionService {
@Resource
private ManufacturingRegionDao manufacturingRegionDao;
@ResponseBody
@Transactional(readOnly = true)
@RequestMapping(value = "/region-codes/{abbr}", method = GET, produces = "application/json")
EDITED: So, if the purpose is "to try Spring Boot", I suggest to not generate a WAR file.
编辑:因此,如果目的是“尝试 Spring Boot”,我建议不要生成 WAR 文件。
If you really need to produce a WAR file (keep your code built against SpringBoot and produce a file that you can run in any standard servlet container...) then you should read the documentation Converting a Spring Boot JAR Application to a WAR.
如果您确实需要生成一个 WAR 文件(保持您的代码针对 SpringBoot 构建并生成一个可以在任何标准 servlet 容器中运行的文件...),那么您应该阅读文档将 Spring Boot JAR 应用程序转换为 WAR。
Did you include spring-boot-maven-plugin in the build process? You didn't describe this step...
您是否在构建过程中包含 spring-boot-maven-plugin ?你没有描述这一步...
回答by Sandeep M
Below code worked fine for tomcat8 deployment without tomcat dependency.
下面的代码适用于没有 tomcat 依赖的 tomcat8 部署。
##代码##}
}
Below dependency is not required.
下面的依赖不是必需的。
##代码####代码##
http://localhost:8080/manufacturing-region-api/manufacturing-region-service/region-codes/ABBRVALUE
http://localhost:8080/manufacturing-region-api/manufacturing-region-service/region-codes/ABBRVALUE