Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy Spring Boot to Tomcat

So I am new to Spring - so I thought I would try 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.

However when i try to package it up as a WAR to deploy to a server it doesn't work

So i thought i would go back to basics and deploy it onto my tomcat server locally and see what happens

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

@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)

however when i try and call the service localhost:8080/service i still get a 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

  1. changed the pom xml package type to war
  2. included the override in the code snippet above
  3. exported as a war

i have unzipped the WAR and it looked fine - there is a WEB-INF and a set of dependencies, etc.

any help would be good - as anyone had similar problems when deploying spring boot apps as a 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

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$000(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.access$000(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$000(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 
like image 469
philthomas26 Avatar asked Jul 14 '14 17:07

philthomas26


People also ask

Can we deploy spring boot application in tomcat?

By using Spring Boot application, we can create a war file to deploy into the web server. In this chapter, you are going to learn how to create a WAR file and deploy the Spring Boot application in Tomcat web server.

Does spring boot require tomcat?

Spring Boot has a complete Tomcat inside. It builds a so-called fat-jar with everything needed inside. You don't need Tomcat installed in your system.

Can I deploy a Spring Boot application war to a standalone Tomcat?

There may be a standalone Tomcat server that should be used instead. In this article, we'll cover how to deploy a Spring boot application WAR to an standalone Tomcat instance. First, we need to tell Spring Boot to not use its embedded Tomcat as the servlet container for the application.

How do I create a Spring Boot project in Tomcat?

Create a Spring Boot Project for Tomcat The most popular way to start a Spring project is with Spring Initializr. Navigate to start.spring.io in your favorite web browser, then choose your project options: Leave as Maven, Java, and the latest stable Spring Boot (2.4.4)

How to write rest endpoint in Spring Boot using Tomcat?

Now, write a simple Rest Endpoint in Spring Boot Application class file using the code as shown below − Now, create a WAR file to deploy into the Tomcat server by using Maven and Gradle commands for packaging your application as given below − For Maven, use the command mvn package for packaging your application.

How do I remove the Tomcat server in Spring Boot?

Remove the embedded Tomcat server by adding the following to your dependencies list: Finally enable your application as a servlet by extending your main class with SpringBootServletInitializer:


1 Answers

The chapter Packaging executable jar and war files in the Spring Boot reference documentation states:

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:

<?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> 
like image 115
James Avatar answered Oct 05 '22 20:10

James