首页 > 解决方案 > 如何在 Spring Boot 中获取请求的 url

问题描述

我有以下控制器:

@Controller
public class MyErrorController implements ErrorController {

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request, Model model) {

        Object url = request.getRequestURL()
        model.addAttribute("url", url)
        return "404";
    }

}

但是,即使用户访问了页面“ http://mywebsite.com/does-not-exist ”,上面的 url 总是给我 url“/error ”。在生成错误页面之前,如何检索用户输入的 url?

标签: javaspring

解决方案


您应该能够从 request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI) 中提取它

该线程有更多信息:web.xml 404 redirect to servlet, how to get the original URI?


推荐阅读