2

I am building a Java Spring boot project - and as a requirement I need to generate the project as a war file - and then have Tomcat Apache run from this alone.

I've built the war file, but when I try to deploy it, its as if the structure is incorrect and its generating a 404 trying to view the project.

  • the problem enter image description here

enter image description here enter image description here

  • my pom

    http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>
    
    <dependencies>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <!-- jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <!-- mysql connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- mail service -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>       
        <!-- hot swapping, disable cache for template, enable live reload -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>provided</scope>
        </dependency>
        <!-- freemarker template -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.23</version>
        </dependency>
        <!-- json simple -->
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
    
        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.194</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    
    <properties>
        <java.version>1.8</java.version>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

The Old County
  • 89
  • 13
  • 59
  • 129
  • Update you question with web.xml & project directory structure with your trying 404 URL –  Oct 12 '17 at 00:33
  • -- Well the web.xml appears to be missing – The Old County Oct 12 '17 at 00:36
  • https://stackoverflow.com/questions/3136177/maven-error-while-building-war-package-web-xml-missing – The Old County Oct 12 '17 at 00:42
  • This is reason you getting 404 because your application server unable to map project or If you don't need web.xml then https://stackoverflow.com/questions/37960038/deployed-simple-helloworld-with-spring-mvc-app-without-web-xml-gives-404-error . –  Oct 12 '17 at 00:46
  • Once you include the dependency of **spring-boot-starter-web**, Spring will automatically add embedded Tomcat as default web container for you. Why do you deploy your application to Tomcat again? – LHCHIN Oct 12 '17 at 00:52
  • not sure -- I have the controller on my application? – The Old County Oct 12 '17 at 00:52
  • but its a rest controller – The Old County Oct 12 '17 at 00:52
  • Well the web app needs to be deployed to a hospital server - so they can only take it as a war file. – The Old County Oct 12 '17 at 00:53
  • Check tomcat logs in file catlina.out. It will give you the full stack trace of this error. – Mandeep Singh Oct 12 '17 at 02:15
  • I will try and look into this - – The Old County Oct 12 '17 at 02:17
  • I have a question for you, before you are running or deploying war. Are you able to run spring boot app from IDE properly. And in spring boot you do not need web.xml to have your app working. – Acewin Oct 12 '17 at 04:40
  • Can you post what does your view resolver look like. And what is your Class file running the SpringBoot look like. – Acewin Oct 12 '17 at 04:46
  • I can run the application through maven - and eclipse ok - so it does the maven build and it runs off the apache tomcat as such - so I assume it builds the jar file and runs that. I can show you the repo in private chat? – The Old County Oct 12 '17 at 04:48
  • You need a ViewResolver or a templating engine like thymeleaf to get it working if you are packaging as a jar. and if you are trying to deply into tomcat you need a web.xml. If you do not have a web.xml then you will need to add it. – Acewin Oct 12 '17 at 04:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156515/discussion-between-acewin-and-the-old-county). – Acewin Oct 12 '17 at 04:54

4 Answers4

7

To get Spring Boot to deploy as a war - you need to reconfigure the Application class.

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html#howto-create-a-deployable-war-file

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}

in the pom.xml add

<packaging>war</packaging>

as I was using maven I also added the dependency.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

My application was complicated in that I was using reactjs as a frontend presentation layer and the Java just as an api layer. I had to move the reactjs build into main/src/webapps -- this then gets rendered in the war file correctly as bundle of js, css and html files. With the reactjs acting as a presentation layer it could only connect to the java side via the api - so the url structure is important and I had placed everything in anticipation it would run from the root. So if you deploy to tomcat - you may need to rename the war file ROOT.war.

The Old County
  • 89
  • 13
  • 59
  • 129
1

Java Spring Boot - war file not deploying on Tomcat Apache

Spring-Boot builed with application.jar including application server inside.

Spring Boot aims to make it easy to create Spring-powered, production-grade applications and services with minimum fuss. It takes an opinionated view of the Spring platform so that new and existing users can quickly get to the bits they need. You can use it to create stand-alone Java applications that can be started using ‘java -jar’ or more traditional WAR deployments. We also provide a command line tool that runs ‘spring scripts’.

https://spring.io/blog/2013/08/06/spring-boot-simplifying-spring-for-everyone

So, You don't need to again deploy in Apache Tomcat Application Server. Just Run It like simple java .jar file.

nohup java -jar app.jar &
  • I can't man - they want it packaged as a war file - so it can be placed on a secure hospital server. I was running the project locally with just maven builds and running the jar file, most likely like you just listed. But they want it to work off just the war file to complete deployment. – The Old County Oct 12 '17 at 02:33
  • The Spring team created SpringBoot to address the complexity of configuration. So you can remove SpringBoot configuration build it as .war by removing pom.xml dependencies configure and annotation , It'll work fine. –  Oct 12 '17 at 02:39
  • -- just remove spring boot configuration from where? - I build it as a war file - it produces a 404 error – The Old County Oct 12 '17 at 02:44
  • From your project pom.xml configuration dependencies & @annotation from code & also remove **main** method. –  Oct 12 '17 at 02:49
  • remove what exactly to actually try and build a usable war file? its built using Spring Boot? – The Old County Oct 12 '17 at 02:57
  • You can't do **anything** with existence .war, It's bad idea to edit .war For solution either you run on cmd as java -jar app.jar & **OR** Rebuild the project excluding SpringBoot configuration & code :( –  Oct 12 '17 at 03:22
  • It just needs to be packaged as a deployable war file and put on a secured firewall server. – The Old County Oct 12 '17 at 04:46
  • @TheOldCounty I think you want to run this application on server not just needs to be packaged as a deployable war, Without correct configuration structure & correct map class package It is Impossible deploy and run this application on application server, While you trying to do it manually, I don't have idea on this –  Oct 12 '17 at 05:55
  • I've created a src/main/webapp folder and moved the reactjs build into there - the application again works fine in my eclipse build -- but when it gets pushed to a war file - I do now see the reactjs frontend, but its blocked on gaining access to the api layer - so when the war variant tries to call api calls root/api/getHome - it returns a 404 - possibly due to reactjs routing denying the existence of such files. – The Old County Oct 12 '17 at 11:14
  • 1
    -- I think I solved it SamDev -- had to re-jig the Application.java "public class Application extends SpringBootServletInitializer" – The Old County Oct 13 '17 at 00:06
  • @TheOldCounty Great But I'm surprise friend, How did you do that?? If possible please answer your question yourself. It might help other user while facing same type issue. –  Oct 13 '17 at 10:11
0

A working example of Spring Boot web project Spring Boot WebApp as WAR

A regular SpringBoot app build through maven is a packaged jar.

You need to modify it as WAR and check if the content of WEB-INF is correct or not.

Spring boot application packaged as war is not for deploying separately into Tomcat but to run directly through the Spring Boot App(through main method). You need to test but as per my understanding if someone is suggesting you to deploy it inside a tomcat then you need to inform them they asking for a regular Web Application and not a SpringBoot application.

Each SpringBoot Application runs its own container.

Additionally you are keeping your view files in template directory, you cannot pick them from template directory as a static content if you are not using Thymeleaf.

Acewin
  • 1,657
  • 4
  • 17
  • 36
0

According to Traditional deployment page you have to add dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>
B. Pleshakov
  • 96
  • 10