一尘不染

如何从Spring Boot提供静态HTML?

spring-boot

我从这里运行spring-
boot-sample-web-static项目,对pom进行了更改

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

并添加了此类,以从相同的static文件夹位置提供重复的页面index2.html :

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Rester {

    @RequestMapping(value = "/rand", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    private RandomObj jsonEndpoint() {
        return new RandomObj();
    }

    @RequestMapping(value = "/tw")
    public String somePg() {
        return "index2";
    }
}

json url工作正常,但是当我尝试访问localhost:8080 / tw时,我得到了一个空白页,并且在控制台中出现此错误:

2017-02-22 15:37:22.076 ERROR 21494 --- [nio-8080-exec-9] o.s.boot.web.support.ErrorPageFilter     : Cannot forward to error page for request [/tw] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

难道我做错了什么?


阅读 540

收藏
2020-05-30

共1个答案

一尘不染

静态文件应从资源而不是控制器提供。

Spring Boot将自动添加位于以下任何目录中的静态Web资源:

/META-INF/resources/  
/resources/  
/static/  
/public/
2020-05-30