简体   繁体   中英

Can't access static resources with Spring Boot

According to the Spring Boot documentation, resources located in the static folder will be served up automatically by Spring Boot with no additional configuration or request mapping.

I am trying to access a javascript file by going to http://localhost:8080/js/filter.js


Here is a screenshot of that section of that part of the Spring documentation.

春季文档截图


Here is a screenshot of my current resources directory structure .

目录结构


Here is my pom.xml to show that I'm using the latest Spring Boot release.

<?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.example</groupId>
    <artifactId>velocity-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>velocity-demo</name>
    <description>Demo project for Spring Boot</description>

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Here is my Application class

package com.example

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

@SpringBootApplication
public class VelocityDemoApplication {

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

}

In my controller I changed @PostMapping to @PostMapping("/foo") and now I can get all of my resources. It was essentially blocking all my requests.

I'll leave this question up, unless of course it gets deleted, because I never would have thought that the controller being configured that way would be blocking all other requests. Someone else may have this issue one day.

@RestController
public class MainController {

    TemplateService templateService;

    public MainController(TemplateService templateService) {
        this.templateService = templateService;
    }

    @GetMapping("/")
    public String getIndex() {
        return templateService.getTemplate("index.vm");
    }

    @PostMapping("/updateFilter") // was @PostMapping
    public String updateFilter() {
        System.out.println("updating filter");
        return "success";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM