0

I have a Spring boot rest service project which works on my local machine. When I run the application as "Spring Boot App"

I can access the rest service by going to

http://127.0.0.1:8080/persons/all

and it returns JSON as it's supposed to.

I changed the pom.xml packaging to war, I then created a war by going to Run as -> Maven build in Spring tools suit. It creates a war file. When I upload the war file on http://myserverip:8080/manager/ I get no errors and it shows up under Applications

The war file name is "myapp-0.0.1-SNAPSHOT.war" after uploading it, I tried to access it by going to

http://myserverip:8080/myapp-0.0.1-SNAPSHOT/persons/all

But I get a 404 error

What am I doing wrong here?

my 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.mycompany</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>myapp</name>
    <description>myappapi</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.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>
    </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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <packaging>war</packaging>
</project>

When I build the application, I get the following logs

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building myapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myapp ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myapp ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myapp ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myapp ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ myapp ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-war-plugin:2.6:war (default-war) @ myapp ---
[INFO] Packaging webapp
[INFO] Assembling webapp [myapp] in [C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [586 msecs]
[INFO] Building war: C:\Users\me\Documents\workspace-sts-3.8.2.RELEASE\myapp\target\myapp-0.0.1-SNAPSHOT.war
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.4.2.RELEASE:repackage (default) @ myapp ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.300 s
[INFO] Finished at: 2016-11-21T10:12:03-05:00
[INFO] Final Memory: 20M/243M
[INFO] ------------------------------------------------------------------------
[WARNING] The requested profile "pom.xml" could not be activated because it does not exist.

my MyappApplication.java

package com.mycompany;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class MyappApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyappApplication.class, args);
    }
}
Arya
  • 8,473
  • 27
  • 105
  • 175
  • You can change your buid name to myapp in pom.xml. Check if the `/persons/all` is registered while server is starting up. If not it will show a 404 error as your controller is not registerd properly. – Lucky Nov 21 '16 at 06:44
  • @Lucky but the application works fine on my local machine. Would it possible that it would register locally with Spring boot, but not register when deployed on Tomcat with a war file? – Arya Nov 21 '16 at 07:53
  • Yes, please check your tomcat server logs. Spring boot uses embedded tomcat. You are deploying into a separate tomcat instance. Both are different. – Lucky Nov 21 '16 at 08:39
  • @Lucky after starting tomcat I do see that myapp is being deployed, but nothing about /persons/all http://pastebin.com/9iyPxuBs – Arya Nov 21 '16 at 15:30
  • I expect your components, configurations, service to be under the `company` package. If not have those inside this package. Also Instead of using the three `@Configuration`, `@EnableAutoConfiguration` and `@ComponentScan` annotations, use this single `@SpringBootApplication` annotation. Also add `spring-webmvc` dependency to your webapp. This flags the application as a web application and activates key behaviors such as setting up a `DispatcherServlet`.(i.e. similar to adding `@EnableWebMvc`) – Lucky Nov 22 '16 at 06:56
  • But, AFAIK you deploy Spring Boot application in an embedded Tomcat container provided by sprint boot itself, instead of traditional standalone tomcat instance. `Make jar not war ;)`. If you still want to make traditional war file and deploy it refer this https://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html – Lucky Nov 22 '16 at 07:03

3 Answers3

2

Refer: Packaging executable jar and war files

As mentioned in comments, you can deploy spring boot application traditionally by deploying war to a standalone tomcat instance. You need to make some changes to your MyappApplication.java class which has the main() method.

  • Make your MyappApplication to extend SpringBootServletInitializer and override its configure() method,

    @SpringBootApplication
    public class MyappApplication extends SpringBootServletInitializer {
    
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }
    
        public static void main(String[] args) throws Exception {
            SpringApplication.run(MyappApplication.class, args);
        }
    
    }
    
  • The next step is to have packaging as war instead of the default jar which you already have.

  • Finally in your pom.xml change the embedded tomcat container's scope to provided, so that it doesn't conflict when you deploy to a separate instance.

Lucky
  • 16,787
  • 19
  • 117
  • 151
  • thank you it works now :) but SpringBootServletInitializer shows up as depreciated. – Arya Nov 22 '16 at 16:12
  • @Arya You might be using `org.springframework.boot.context.web.SpringBootServletInitializer` use `org.springframework.boot.web.support.SpringBootServletInitializer` instead. – Lucky Nov 23 '16 at 06:39
1

if you are changing jar to war of spring boot add following 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>

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
1

Checklist for changing a spring boot executable jar into a deployable war.

  1. Change packaging in pom.xml to "war" from "jar"

  2. As kuhajeyan mentioned, mark your embedded tomcat dependency as provided in the pom.xml.

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

  3. Make your main spring boot application class extend SpringBootServletInitializer like this

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

From the documentations

ameenhere
  • 2,203
  • 21
  • 36