gpt4 book ai didi

spring-mvc - 如何找出请求到达错误处理程序的 url?

转载 作者:行者123 更新时间:2023-12-02 06:55:13 50 4
gpt4 key购买 nike

我发送以下 http 请求:

http://localhost:8081/member/createCompany/getSmallThumbnail/

在服务器端,我进入了 Controller 方法:

@RequestMapping("/error")
public String error(Model model, HttpServletRequest request){
if(request.getRequestURI().contains("thumbnail")){
System.out.println("thumbnail accepted");
}
request.toString();
model.addAttribute("message", "page not found");
return "errorPage";
}

在这个方法中我想知道请求到达的 url。

如果在调试中我在这个方法中停止,我会看到我需要的信息: enter image description here

但是我在请求中找不到返回这个的方法。

请帮忙返回我想要的url。

附言

实际上,我还没有在我的 spring mvc 应用程序(url 已损坏)中为 http://localhost:8081/member/createCompany/getSmallThumbnail/ 映射 Controller 。此 url("/error") 在 web.xml 中配置为错误页面。

最佳答案

你的请求被重新发送到 /error(可能是为了错误处理)。

如果此框架遵循正常的 Servlet 错误调度行为,那么您的原始请求可以在各种 javax.servlet.RequestDispatcher.ERROR_* 下的 HttpServletRequest.getAttributes() 中找到。键。

  • ERROR_EXCEPTION - 异常对象
  • ERROR_EXCEPTION_TYPE - 异常对象的类型
  • ERROR_MESSAGE - 异常消息
  • ERROR_REQUEST_URI - 导致错误分派(dispatch)的原始请求 uri
  • ERROR_SERVLET_NAME - 导致错误的 servlet 的名称
  • ERROR_STATUS_CODE - 为此错误分派(dispatch)确定的响应状态代码

你想要的是

String originalUri = (String) request.getAttribute(
RequestDispatcher.ERROR_REQUEST_URI)

关于spring-mvc - 如何找出请求到达错误处理程序的 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910492/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com