2017-04-03 91 views
0

根据Spring Boot文档,位于static文件夹中的资源将由Spring Boot自动提供,无需其他配置或请求映射。无法通过Spring Boot访问静态资源

我试图去访问一个JavaScript文件http://localhost:8080/js/filter.js


这里是Spring文档的一部分的该部分的屏幕截图。

spring documentation screenshot


这是我当前resources目录结构的屏幕截图。

directory structure


这里是我的pom.xml表明我使用最新的Spring引导释放。

<?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> 

这里是我应用类

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); 
    } 

} 
+0

你表现得很好。该问题可能是由您的Spring配置中的某些内容引起的。你能展示一切吗? – davidxxx

+0

没有进一步的配置。我会将我的Application类添加到帖子中,但它是香草类。 – qbolt

+0

确实。这很简单。但它是全**吗?我刚刚测试过,它工作。 – davidxxx

回答

0

在我的控制,我改变@PostMapping@PostMapping("/foo"),现在我可以让我的所有资源。它基本上阻止了我的所有请求。

我会离开这个问题,除非当然它会被删除,因为我从来不会想到控制器配置的方式会阻止所有其他请求。其他人可能有一天会遇到这个问题。

@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"; 
    } 
}