Java handleNoSuchRequestHandlingMethod重写不工作
我的代码中有这个,但是在404的情况下,它从未被调用过。 (我的web.xml中没有定义错误页,我也不想)您需要对方法使用注释:Java handleNoSuchRequestHandlingMethod重写不工作,java,spring-mvc,overriding,http-status-code-404,Java,Spring Mvc,Overriding,Http Status Code 404,我的代码中有这个,但是在404的情况下,它从未被调用过。 (我的web.xml中没有定义错误页,我也不想)您需要对方法使用注释: @Controller public class CentralizedExceptionController extends DefaultHandlerExceptionResolver { @Override protected ModelAndView handleNoSuchRequestHandlingMethod(NoSuchReque
@Controller
public class CentralizedExceptionController extends DefaultHandlerExceptionResolver {
@Override
protected ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("working?!");
return new ModelAndView();
}
看看这个jira问题:如果您的Spring DispatcherServlet配置为处理所有/大多数URL,那么您可能会从控制台收到404错误以及DispatcherServlet日志消息:
@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
public ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
...
}
这表明Spring的DispatcherServlet正在处理该请求,但没有合适的@RequestMapping来分派
一个简单的解决方案是通过将web.xml的servlet映射>url模式
重新配置为仅由应用程序的@RequestMappings指定的url来限制dispatcher servlet处理的请求。但是,这不是很实际(所以不要这样做)
克服这一问题的一种方法是创建一个@RequestMapping来处理所有“未处理”的请求映射—某种回退请求映射
No mapping found for HTTP request with URI [xxx]
请注意,这个答案在方法上与Dani的答案类似,但在编写时考虑了基于注释的开发。因此,了解相关的plz检查非常有用。控制器类名应不是controller.java。这不起作用。仅当您将此方法放在创建404的同一类中时,它才起作用。“您可以创建一个处理程序,将其映射到所有传入请求,并按照最后一行的顺序进行排序。例如,实现HttpRequestHandler,使用SimpleUrlHandlerMapping将其映射到“/**”,并将SimpleUrlHandlerMapping的order属性设置为Integer.MAX\u值(默认设置)。这基本上是未处理请求的总括信息。“你能给我举个例子吗?很抱歉,我现在无法测试,但我希望这能给你一个线索:
@RequestMapping("**")
@ResponseBody
public String fallbackRequestMapping() {
return "do something useful...";
}