我从 这里 运行了 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
难道我做错了什么?
原文由 osmingo 发布,翻译遵循 CC BY-SA 4.0 许可协议
静态文件应该从资源中提供,而不是从控制器中提供。
参考:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
https://spring.io/guides/gs/serving-web-content/