是否可以设置错误页面但是应该不是针对所有URI触发,而是使用/ custom-path触发,对于具有URI / main-path的请求不能处理错误?
在web.xml中,我认为只能将错误页面与某些异常类型匹配。
问题是我们有一个单独的war文件,其中我们有api和html管理UI。因此,如果错误来自rest api,它应该按原样呈现,并且不应显示错误页面。
答案 0 :(得分:0)
我遇到了同样的问题,我通过在HttpServletResponse上调用setStatus()而不是抛出异常或调用sendError()来解决它。
我的Spring MVC控制器示例:
@RequestMapping(value = "/{ean}", method = RequestMethod.GET)
@ResponseBody
public User getUser(@PathVariable("id" Long id, HttpServletResponse response) {
// response.sendError(HttpServletResponse.SC_NOT_FOUND)
response.setStatus(HttpServletResponse.SC_NOT_FOUND)
}
这样我的RESTful API控制器就不会呈现错误页面。