Spring Boot Error控制器的处理程序错误

问题描述

我已经遇到了一段时间了,但是我似乎找不到找到此模棱两可的错误的原因。

这是我得到的错误:

Sep 25 21:23:27 ip-172-31-29-87 web: java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path 'http://172.31.29.87/error': {public org.springframework.web.servlet.ModelAndView com.sample.controller.WebsiteController.error404() throws java.lang.Exception,public org.springframework.http.ResponseEntity org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)}

这是我的http配置:

.and().exceptionHandling().accessDeniedPage("/AccessDenied").

这是我的/ AccessDenied控制器:

@RequestMapping(value = "/AccessDenied")
public ModelAndView accessDenied() throws Exception {
    ModelAndView model = new ModelAndView("accessDenied");
    
    return model;
}

真的希望您能帮助我。谢谢。

解决方法

Spring找不到处理错误的终点。我认为您应该为@RequestMapping指定Http方法。另外,this answer 可能会有用。

更新 另外,我发现here可以用accessDeniedPage()方法指定html页面。例如,您可以创建error.htmlerror.jsp并将其添加到代码.and().exceptionHandling().accessDeniedPage("/error.jsp")中。在这种情况下,您不再需要在控制器中处理此请求。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...