Spring Boot not updating static html (with Kotlin/

2019-08-16 12:26发布

问题:

I built a pretty simple static content web server using Spring Boot, Kotlin and Intellij. I run my program and it properly serves my html on localhost:8080. However, if I update that html while the server is running then force reload the page in the browser my changes are never shown. I've read a bunch of things that could be the problem. Something about thymeleaf cache. Spring Boot Devtools. Intellij auto builds or something. Nothing seems to work. Maybe an extra set of eyes on my code could pick up something I'm missing.

Here's my build.gradle

buildscript {
    ext.kotlin_version = "1.2.0"
    ext.httpcomponents_version = "4.5.4"
    ext.springframework_version = "1.5.9.RELEASE"

    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.springframework.boot:spring-boot-gradle-plugin:$springframework_version"
    }
}

group "com.atomicslurry"
version "1.0"

apply plugin: "java"
apply plugin: "kotlin"
apply plugin: "spring-boot"

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    compile "org.apache.httpcomponents:httpclient:$httpcomponents_version"
    compile "org.apache.httpcomponents:fluent-hc:$httpcomponents_version"
    compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.2"
    compile "org.springframework.boot:spring-boot-starter-thymeleaf:$springframework_version"
    compile "org.springframework.boot:spring-boot-devtools:$springframework_version"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

Here's my kotlin app main

package com.atomicslurry.sentinel

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication

@SpringBootApplication
open class App

fun main(args: Array<String>) {
    SpringApplication.run(App::class.java, *args)
}

Finally, my static html is in src/main/resources/static

Any ideas?

回答1:

The general workflow that is working for me:

  1. Edit HTML
  2. Press Build Project (Ctr+F9) in IntelliJ, then wait until changes completed
  3. Go to Browser and refresh


回答2:

If you're using Spring Boot with static resources at src/main/resources/public, this can be done by add the following to application.properties:

spring.resources.static-locations[0]=file:src/main/resources/public/
spring.resources.static-locations[1]=classpath:/public/

The "file:" makes the content update on refreshing the browser, see related issue.

Alternatively, the file resource locations can be discovered at runtime and added programmatically.

See also documentation and tutorial.