Piotr Bartoch
Piotr Bartoch

Reputation: 537

Spring Boot WAR file deployment on tomcat shows Error 404

I try to deploy Gradle + Spring Boot 2 + Angular2 app WAR file on standalone Tomcat 7.0.91. If I boot application with provided jar and embedded tomcat everything works ok, but when deploying on standalone container when I go to "/springboot" context it gives me 404: "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." Logs from tomcat don't tell anything usefull. I tried 2 approaches of deployment, by setting in server.xml context path and doc base:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Context path="/springboot" docBase="/home/user/SideProjects/springboot-angular2-gradle-war/build/libs"/>                                                     
</Host>

And second was deploying war file "by hand" in webapps directory.

I'm not sure but file context.xml in tomcat is set to watch resources in WEB-INF/web.xml but as I know, spring boot doesn't use web.xml anymore so maybe that's the case? Unfortunatelly every tutorial i found didn't tell anything about changing context.xml, moreover i can't find what resource in spring is considered as deployment descriptor nowadays.

That's my build.gradle file:

 buildscript {
    ext {
        springBootVersion = '2.0.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

apply plugin: 'application'
mainClassName = "com.demo.SpringbootAngular2GradleWarApplication"

group = 'com.demo'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

war {
    baseName = "demo-spring"
}

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}


def frontendDir = "$projectDir/src/main/frontend"
def frontDistDir = "$frontendDir/dist"

sourceSets {
    main {
        resources {
            srcDirs = ["$frontDistDir", "$projectDir/src/main/resources"]
        }
    }
}

clean.doFirst {
    delete "$frontDistDir"
    println "$frontDistDir - cleaned"
}

task buildAngular(type: Exec) {
    workingDir "$frontendDir"
    inputs.dir "$frontendDir"
    commandLine "npm", "run-script", "build"
}

processResources.dependsOn "buildAngular"

Main class extends servlet initializer:

 package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringbootAngular2GradleWarApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringbootAngular2GradleWarApplication.class);
    }

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

Upvotes: 1

Views: 2539

Answers (1)

Piotr Bartoch
Piotr Bartoch

Reputation: 537

Problem was with versions incompatibility, minimum Tomcat version supported by SpringBoot-2 is 8.5.x in order to el-api and more. Actually dropping war in webapps and renaming it "by hand" works.

Upvotes: 1

Related Questions