gtludwig gtludwig - 3 months ago 98
Java Question

Spring boot - why can't my application find static resources?

I am giving Spring Boot a go and can't access CSS, images and JS files.

I've this @SpringBootApplication class:

@SpringBootApplication
public class Application {

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


My
application.properties
file reads:

spring.datasource.url=jdbc:mysql://localhost:3306/<schema>?autoReconnect=true&useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.name=<sql-databasename>
spring.datasource.username=<sql-username>
spring.datasource.password=<sql-password>
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=update

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.context-path=/<application-name>
server.display-name=<application-name>
server.port=8080
server.session.cookie.max-age=3600

# SPRING MVC (WebMvcProperties)
spring.mvc.favicon.enabled=true
spring.mvc.date-format=dd/MM/yyyy
spring.mvc.locale=pt_BR
spring.mvc.locale-resolver=accept-header
spring.mvc.servlet.load-on-startup=-1
spring.mvc.static-path-pattern=/**

# SPRING RESOURCES HANDLING (ResourceProperties)
spring.resources.add-mappings=true
spring.resources.cache-period=1
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/**,classpath:/public/**

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html


Purely by still not fully understanding how Spring Boot works, I haven't been able to shake this configuration file off:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "b.c.g.c")
public class CmsBootApplicationConfiguration extends WebMvcConfigurerAdapter {

@Bean(name = "messageSource")
@Description("Spring message resolver")
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("utf-8");
return messageSource;
}

@Bean(name = "localeResolver")
@Description("Spring locale resolver")
public LocaleResolver localeResolver() {
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(new Locale("pt", "BR"));
return resolver;
}

@Bean(name = "multipartResolver")
public StandardServletMultipartResolver resolver() {
return new StandardServletMultipartResolver();
}

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

@Bean
public UrlTemplateResolver urlTemplateResolver() {
return new UrlTemplateResolver();
}
}


Static resources are located here:

src/main/resources
static
css/
img/
js/


But, when I try to access any static resource like, like, for instance on the browser call bellow:

http://localhost:8080/<application-name>/static/css/bootstrap.min.css


The result is:

{"timestamp":1472734923645,"status":404,"error":"Not Found","message":"No message available","path":"/cms/static/css/bootstrap.min.css"}

But the file is there...

I glanced through but couldn't find the solution at the Spring Boot Reference Guide.

What am I doing wrong and how to get it working?

Answer

Remove the spring.resources.static-locations from your config unless you want to change it, /static/ is included by default.
Resourced inside the static directory are included on the http path.
When referencing the resources you don't include static.
The file src/main/resources/static/css/bootstrap.min.css should be available on http://localhost:8080/appname/css/bootstrap.min.css

Comments