'spring.resources.static-locations not working with devtools
I have a project with the following structure (just like spring.io):
├── frontend
│ ├── dist
│ | ├── app.js
│ | ├── vendor.js
│ | ├── style.css
│ | ├── index.html
│ ├── node_modules
│ ├── src
│ ├── package.json
│ ├── pom.xml
├── site
│ ├── src
│ ├── pom.xml
frontend is a NodeJS project and site is a Spring Boot project.
All files inside frontend/dist (including html) are autogenerated by webpack. In order to access
them from Spring Boot app I've configured development profile.
application.yml
spring:
profiles: development
resources:
static-locations:
- file:../frontend/dist/
All works just fine. But when I add spring-boot-devtools into the site project classpath I get 404 for any file inside dist folder.
http://localhost:8080/ -> 404
http://localhost:8080/app.js -> 404
Again, no devtools - no problem.
Also if I replace static-locations with addResourceHandlers it works too (no matter with or without devtools).
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("file:/full/path/to/frontend/dist/");
}
Is this a devtools bug or my fault?
UPDATE:
Ok thanks to @AndyWilkinson I've figured out the cause of the problem. I work in IntelliJ and use run configurations to set env variables. Here is the resulting command line:
/usr/lib/jvm/java-8-oracle/bin/java -bla-bla-bla -Didea.version=2017.1.3 -T 2 -Dspring.profiles.active=dev spring-boot:run
But if devtools is in the project classpath Spring Boot ignores them:
Could not find key 'spring.profiles.active' in any property source
No active profile set, falling back to default profiles: default
Now if I simply remove devtools from pom.xml
The following profiles are active: dev
Activated profiles dev
... wow, it works again.
The possible workaround is to set active profile directly through application.yml.
spring:
profiles:
active: dev
---
spring:
profiles: dev
resources:
static-locations:
- file:../frontend/dist/
.. but i don't like it because it's inconvenient.
So because devtools is such a pain I think I'll prefer to use F5 as before.
Solution 1:[1]
I had similar problem.
I added spring.resources.static-location=file:../frontend/build in application.properties
Use can also add absolute path
spring.resources.static-location=file:/User/XYZ/Desktop/frontend/build
Solution 2:[2]
Configure InternalResourceViewResolver to resolve static resources too:
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/classes/static/");
viewResolver.setSuffix("");
return viewResolver;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LAXIT KUMAR |
| Solution 2 | Elletlar |
