spring-boot - Spring Boot 中的 HTTP 响应过滤器
问题描述
我有一个像http://example.com这样的网站,并且我的网站下有很多 URL(http://example.com/a、/ab、/abc等)。
另外假设spring boot项目中我的资源文件下有很多视图文件(x.html、y.html等)。
我将它部署在 Centos 服务器的 Tomcat9 中。
我想建立类似的东西:
- if (HTTPResponse!=200) -> 路由到 x.html (请求的所有响应传入我项目中的所有 URL)
- else -> 处理正常的 HTTP 请求响应流
有一些方法:在课堂上处理它Interceptor
,Filter
课堂上,也@ControllerAdvice
做一些我想要的(我建造的)。
所以我不想在我的网站上显示 HTTP 错误。如果它有一个 HTTP 错误,将它路由到我的 x.hmtl 页面,否则处理常规的 Req-Resp 流。
构建它的最佳方法是什么?你有什么建议?
解决方案
试试这个方法
@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(Exception.class)
public ModelAndView handleError(HttpServletRequest request, Exception e) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Request: " + request.getRequestURL() + " raised " + e);
return new ModelAndView("error");
}
@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handleError404(HttpServletRequest request, Exception e)
{
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Request: " + request.getRequestURL() + " raised " + e);
return new ModelAndView("404");
}
}
推荐阅读
- python - Django,2.0.4 路径不起作用,如何解决?
- android - 该命令将用于反应原生的 apk 放在哪里?
- java - 从一个活动获取文本到另一个活动
- node.js - 调用node app.js时出错,[node js socket io]如何解决?
- python - Coremltools:可以解码 XGBoost JSON 对象
- java - 如何修改某些数组元素而不影响其他元素?
- c - execvp() 打印无效选项 -- '
- javascript - 如何按名称从数组中正确删除对象?
- sql - Oracle 将旧列移动到新的用户定义类型
- dart - 调用 setState 时防止 ExpansionTile 重置