Ru Ru Ru Ru - 4 months ago 89
Java Question

Unable to access and use static resources in Spring Boot Application

I have been fighting with getting resources to work for quite sometime now, and I know there are thousands of similar questions.. Anyway, my condition is this

I will start with my pom.. I might have missed something here?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.ruruapps</groupId>
<artifactId>spring-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-demo</name>
<description>Spring-Demo Application</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.thymeleaf-version>2.1.4</org.thymeleaf-version>
<webjars-bootstrap.version>3.3.5</webjars-bootstrap.version>
<webjars-jquery-ui.version>2.1.1</webjars-jquery-ui.version>
<webjars-jquery.version>2.0.3-1</webjars-jquery.version>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Thymeleaf -->
<!--<dependency>-->
<!--<groupId>org.thymeleaf</groupId>-->
<!--<artifactId>thymeleaf</artifactId>-->
<!--<version>2.1.4.RELEASE</version>-->
<!--</dependency>-->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- Webjars (static dependencies distributed as WAR files) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


Here are the errors I am dealing with

Errors loading resources

And in the terminal I see this

Looking up handler method for path /css/bootstrap/css/bootstrap.min.css
Did not find handler method for [/css/bootstrap/css/bootstrap.min.css]
Request method 'GET' not supported


What does this mean?

I have a correct structure and doing it by the convention

Structure

But no luck what so ever. I tried changing to public/ and tried using webjars. All in vain. What are the norms for this and how do I get this set up so it wont be breaking! Thanks!

EDIT:: Configuration Files

Here is my application file that is provided with the spring boot

@SpringBootApplication
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})
@ComponentScan(basePackages = {"webapp"})
@EnableJpaRepositories
public class SpringDemoApplication {

public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringDemoApplication.class, args);
}
}


And I have a WebConfig file which is mostly empty, but I tried adding the @Overide on addResourceHandlers, with no improvements

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"webapp"})
public class WebConfig extends WebMvcConfigurerAdapter {

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


EDIT

After further examining the application, I have found this

Trace output

It says that the request it allows is PUT, but why? From what I can understand is that something is blocking my request to the resource files, is that right?

Desperate to get this fixed, it's really bothering to configure Spring, even with Spring Boot!

Answer

Found a problem that I was facing with not being able to access static resources. For those who experience the same:

This behaviour (405 error) signifies that something is blocking the path to your static resources. This doesn't have to be /static or /public. It wont do anything.

In my case the problem was that a controller was blocking the /css path. To fix this a @RequestMapping(value = "/Somepath") annotation on the class lvl is needed to release the path for the static resources.

This worked for me and now I can access my static resources without any issues.

This can be a very annoying problem and someone can spend hours and days trying to figure this out. Hope this helps others to save their time

If there are alternative or other methods of preventing this behaviour that please mention in the comments