Alex
Alex

Reputation: 2030

Spring Boot returns 404 for static resources with docker

I'm using spring boot with docker. I have static resources in spring boot project and when I runt it with mvn spring:boot run everything works fine and resources are available. But when I create docker image from it using command

FROM openjdk:8-jdk-alpine
ADD /target/example-*.jar /
EXPOSE 8080
ENTRYPOINT java -jar example-*.jar

Then my static resources are not available by the localhost:8080. App started successfully(no errors in logs.)

Upvotes: 1

Views: 4491

Answers (1)

thesb
thesb

Reputation: 219

Resources in a jar file can be tricky.

Here are some tips for trouble shooting:

  1. Is the url for the 404 the same with both methods of deployment?

  2. Inspect the jar file used in docker, are your resources even there?

  3. Try running the jar file without docker, I suspect your resources are missing. If missing, you need to get maven to push them to the jar and you are probably not following the springboot conventions for resources.

Here is what worked for me when I needed to include a full springboot app as a dependency in a parent application:

Location

  • Resources => src/main/resources/META-INF/resources/static
  • View Templates => src/main/resources/WEB-INF/templates

If MVC, make sure you map your resources:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/static/**").addResourceLocations("classpath:META-INF/resources/static/");
    registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:META-INF/resources/webjars/");
    registry.addResourceHandler("/**").addResourceLocations("classpath:META-INF/");
}

Also needed some changes to my gradle build:

buildscript {
    ext {
        springBootVersion = '2.0.0.M7'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
        maven { url 'http://repo.spring.io/plugins-release' }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE'
        classpath 'org.springframework:springloaded:1.2.6.RELEASE'
    }
}

ext {
    springBootVersion = '2.0.0.M7'
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'groovy'
//apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'propdeps'
apply plugin: 'propdeps-idea'

group = 'com.example.app'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

configurations {
    includeInJar
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-groovy-templates')
    compile('org.codehaus.groovy:groovy')

    includeInJar("org.webjars:bootstrap:4.0.0")
    includeInJar("org.webjars:jquery:3.3.1")
    configurations.compile.extendsFrom(configurations.includeInJar)
}

idea {
    module {
        inheritOutputDirs = true
    }
}

jar {
    from configurations.includeInJar.collect { it.isDirectory() ? it : zipTree(it) }
}

compileJava.dependsOn(processResources)

Upvotes: 2

Related Questions