Spring Boot War Deployed at Tomcat

I am trying to deploy a Spring boot application for Tomcat because I want to deploy in AWS. I created a WAR file, but it does not seem to work on Tomcat, although this is visible.

Details:
0. Here is my application:

@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 the following meanings:

 server.port=${port:7777} 
  • After reading a number of pages and question and answer, I added 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 the "mvn package" and got a WAR file (size 250Mb), which I entered in the "webapps" folder.

  • I started Tomcat and was able to see my application in my case "/highlighter-1.0-SNAPSHOT".
  • When you click on the link for the application, the "Status 404" page appears.
  • When I launch the Spring Boot application only on my own, without a container it runs on localhost: 7777, but there is nothing there when I launch it on Tomcat.

Update: There is another reference . Not sure how useful it is.

+68
spring spring-boot maven tomcat
Jan 12 '15 at
source share
11 answers

This guide explains in detail how to deploy the Spring Boot application on Tomcat:
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

Essentially, I needed to add the following class:

 public class WebInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(App.class); } } 

I also added the following property to POM:

 <properties> <start-class>mypackage.App</start-class> </properties> 
+101
Jan 12 '15 at 15:37
source share

I think that you are confused by different paradigms. First of all, military files and server deployments - these things belong to Java Enterprise Edition (Java EE). These concepts have no real place in the spring-boot application that follows another model.

Spring-boot is responsible for creating an inline container and running your services inside it directly from the standard jar files (although it can do much more). I think the goal of this model is to support the development of microservice - where each service has its own container and is completely autonomous. You can use your code to create Java EE applications, but that would be stupid given that spring-boot is much simpler (for certain types of applications / services).

So, given this information, you now need to decide which paradigm you are going to follow, and you need to follow this and only this.

Spring-boot is an executable file - you just need to run the main method in the application class, which you can do from the command line or using your favorite IDE or maven or gradle (tip: maven is right answer). This will bring up the tomcat server (by default) and your service will be available in it. Given what you posted above, your service should be available at: http://localhost:7777/context/help - context intended to replace with a context name that you did not use.

You do not have to create a war, run tomcat or deploy anything. None of this is required in spring-boot. The packaging in your pom should be jar , not war , and the scope spring-boot-starter-tomcat should be removed - it, of course, is not provided.

When you run your main method, the context you have registered should be indicated on the console output; use this for the correct url.

Having said all this, spring-boot should exist in the JEE world at the moment (until it is widely accepted). For this reason, spring people have documented an approach to building a war, not an executable bank, for deployment to a servlet or JEE container. This allows you to use many spring-boot technologies in environments where there are restrictions on the use of anything other than wars (or ears). However, this is only an answer to the fact that such environments are quite common and are not considered as a necessary or even desirable part of the solution.

+20
Jan 12 '15 at 15:29
source share

Hey, be sure to do it, change to pom.xml

 <packaging>war</packaging> 

in the dependencies section, be sure to indicate that tomcat is provided, so you do not need the built-in tomcat plugin.

  <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

 <?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 look like this:

Application.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 to test 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 tomcat 8 and access a controller like this

http: // localhost: 8080 / demo / hi

If for some reason you cannot add a project to tomcat, right-click in the project, and then go to the build path → configure build path-> Project Faces

make sure that only these 3 are selected

Dynamic Web Module 3.1 Java 1.8 Javascript 1.0

+18
Sep 12 '16 at 7:10
source share

Your Application.java class should extend the SpringBootServletInitializer class for example:

 public class Application extends SpringBootServletInitializer {} 
+4
Sep 03 '16 at 6:54
source share

Solution for people using Gradle

Add plugin to build.gradle

 apply plugin: 'war' 

Add subject to tomcat dependency

 dependencies { // other dependencies providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' } 
+4
Jun 19 '17 at 6:48
source share

After executing the manual (or using Spring Initializr), I had a WAR that worked on my local computer, but did not work remotely (works on Tomcat).

There was no error message, he simply said that a "Spring Servlet Initializer" was found, but did nothing.

 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 Download just did not start.

Apparently, I compiled the server with Java 1.8, and there was Java 1.7 on the remote computer.

After compiling with Java 1.7, it started working.

 <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> 
+3
Aug 17 '16 at 15:05
source share

Public class Application extends SpringBootServletInitializer {}

just extends SpringBootServletInitializer. It will work in your AWS / tomcat

+3
Sep 03 '16 at 11:01
source share

I had the same problem and figured out a solution by following this guide . I am running with a goal in maven.

clean package

It worked for me Thanq

+3
Mar 28 '17 at 12:33
source share

If your goal is to deploy the Spring boot application for AWS , Boxfuse gives you a very simple solution.

All you have to do is:

 boxfuse run my-spring-boot-app-1.0.jar -env=prod 

This will:

  • Prevent a minimal OS image specifically designed for your application (about 100x less than a regular Linux distribution).
  • Push it to a secure online repository
  • Convert it to AMI in about 30 seconds.
  • Create and configure a new Elastic IP or ELB
  • Assign it a new domain name
  • Launch one or more instances based on your new AMI

All images are generated in seconds and immutable. They can work without changes on VirtualBox (dev) and AWS (test and prod).

All updates are performed as strong / green deployments with zero downtime , and you can also enable automatic scaling with just one command.

Boxfuse also understands that your Spring boot configuration file automatically configures ELB security groups and health checks based on application.properties .

Here is a tutorial to help you get started: https://boxfuse.com/getstarted/springboot

Disclaimer: I am the founder and CEO of Boxfuse

0
Dec 17 '15 at 14:46
source share

Update 2018-02-03 with Spring Boot 1.5.8. RELEASE.

In pom.xml, you should tell the Spring plugin when it collects that it is a war file by changing the package in war, for example:

 <packaging>war</packaging> 

In addition, you should exclude the built-in tomcat when building the package by adding this:

  <!-- to deploy as a war in tomcat --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> 

A complete launch example is here https://www.surasint.com/spring-boot-create-war-for-tomcat/

0
Feb 03 '18 at 14:50
source share

If you are creating a new application and not converting an existing one, the easiest way to create a WAR boot-based Spring boot application is through the Spring Initializr .

It automatically creates an application for you. By default, it creates a Jar, but in the advanced options you can choose to create a WAR. This war can also be carried out directly.

enter image description here

Even easier to create a project from IntelliJ IDEA directly:

File → New Project → Spring Initializr

0
Jul 19 '19 at 17:50
source share



All Articles