1

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!

Ru Ru
  • 137
  • 1
  • 4
  • 18
  • Do you have configuration classes? Is your application running on/as root? I would suggest using web jars instead of including your own version of bootstrap.Try prefixing your resource urls with `/static` although I would expect this to work with a default configuration. – M. Deinum Jul 03 '15 at 09:23
  • No, I am using AutoConfiguration annocation on the Application class that comes with Spring Boot. I have tried using webjars, but I am not aware to how to configure them. Last time I tried I was getting errors with loading them, it would not compile at all – Ru Ru Jul 03 '15 at 09:35
  • Post your application class. Why wouldn't it compile with web jars? Did you include the correct dependencies? Also when using web jars you would have to change the resource urls obviously. – M. Deinum Jul 03 '15 at 09:37
  • Looks like `` configuration may be missing or incorrectly set. I'd suggest you to add more details to your question. Like project spring configurations, – andriy Jul 03 '15 at 09:41
  • Yes that's what it seems like. Some parts of the application are working just fine, but the resource management does not. How should I go about configuring it manually using Java? – Ru Ru Jul 03 '15 at 09:50
  • As mentioned please add your application class and any configuration you might have. Spring Boot already configures those paths by default if they aren't working you are either disabling them yourself or you aren't using things properly. – M. Deinum Jul 03 '15 at 09:53
  • I have made the updates to the question, please could you have a look? – Ru Ru Jul 04 '15 at 09:55

3 Answers3

4

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

Ru Ru
  • 137
  • 1
  • 4
  • 18
0

If you are trying to configure resource mapping through configuration class as you mentioned in comment, then it should be like this:

@EnableWebMvc 
@ComponentScan(basePackages = {"org.company.yourpackage"})
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
    }
}

this post also may help.

andriy
  • 4,074
  • 9
  • 44
  • 71
  • This didn't do anything either. I have made an update to the question. It might give more clues – Ru Ru Jul 04 '15 at 09:49
0

I was having the exact same issue, What helped me was adding:

enter code here
<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>*.js</url-pattern>
</servlet-mapping>

OR

<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>/js/*</url-pattern>
</servlet-mapping>

from this post servlet for serving static content here

Community
  • 1
  • 1