在Spring MVC中返回NoSuchRequestHandlingMethodException 404错误的自定义视图

时间:2016-04-16 05:36:51

标签: java spring spring-mvc exception-handling

每当用户在浏览器中键入错误的URL时,都会导致NoSuchRequestHandlingMethodException我想返回自定义视图,而不是spring提供的默认视图。我有@AdviceController我试图通过@ExceptionHAndler(NoSuchRequestHandlingMethodException.class)抓住它并返回一个视图,但它没有用。我还继承了DefaultHandlerExceptionResolver并覆盖了处理NoSuchRequestHandlingMethodException的方法,这也没有用。我还尝试通过扩展SimpleMappingExceptionResolver setExecptionMapping(在相同的@AdviceController类)方法中将视图名称注册到例外,这种方法也没有工作。我不知道自己做错了什么。当我在控制器中找不到任何映射时,我是否正确地关注那个春天NoSuchRequestHandlingMethodException可以有人告诉我如何返回与HTTP错误代码匹配的Spring Exceptions的自定义视图。

这是我的@ControllerAdvice

@ControllerAdvice
public class ExceptionHandlerController extends     
DefaultHandlerExceptionResolver {

@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
public String handleException(NoSuchRequestHandlingMethodException.class, Model model) {

    String error = th.getMessage();
    model.addAttribute("error", error);
    model.addAttribute(new User());
    return "login";
   }

@override
protected ModelAndView handleNoSuchRequestHandlingMethodException
(NoSuchRequestHandlingMethodException ex, HttpServletRequest request, 
HttpServletResponse response, Object handler) throws Exception {

      ModelAndView mav = new ModelAndView("notfound");
      return mav;
   }
}

更新:解决方案

不会引发NoSuchRequestHandlingMethodException的原因是因为Dispatcher Servlet将请求委托给默认的servlet处理程序(在我的情况下为DefaultServletHttpRequestHandler)并认为它已被处理。我实现的解决方法是我创建了一个带有映射' / *'的处理程序方法。当在任何控制器中找不到特定映射时执行,并且我通过新的NoSuchRequestHandlingMethodException执行,可以在@ExceptionHandler中捕获,然后返回自定义视图。但是,我不知道像这样处理异常的缺点。如果有的话请告诉我。

@Controller
public class HomeController {

...

@RequestMapping(value = "/*", method = RequestMethod.GET )
public void handleAtLast(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
    throw new NoSuchRequestHandlingMethodException(request);
  }
}

ExceptionHandlerController.java

@ControllerAdvice
public class ExceptionHandlerController extends DefaultHandlerExceptionResolver {
   @ExceptionHandler(NoSuchRequestHandlingMethodException.class)
   public String handleNoSuchMethodtFoundException(NoSuchRequestHandlingMethodException ex){
       return "error/notfound";
    }
}

2 个答案:

答案 0 :(得分:0)

试试这种方式

@ControllerAdvice
public class AppWideExceptionHandler {

    @ExceptionHandler(Exception.class) // replace with your exception Class
    public String errorHandler(){
        System.out.println("caught exception");
        return "home"; // your view
    }
}

答案 1 :(得分:0)

不会引发NoSuchRequestHandlingMethodException的原因是因为Dispatcher Servlet将请求委托给默认的servlet处理程序(在我的情况下为DefaultServletHttpRequestHandler)并认为它已被处理。我实现的解决方法是我创建了一个带有映射' / *'的处理程序方法。当在任何控制器中找不到特定映射时执行,并且我通过新的NoSuchRequestHandlingMethodException执行,可以在@ExceptionHandler中捕获,然后返回自定义视图。但是,我不知道像这样处理异常的缺点。如果有的话请告诉我。

@Controller
public class HomeController {

...

@RequestMapping(value = "/*", method = RequestMethod.GET )
public void handleAtLast(HttpServletRequest request) throws NoSuchRequestHandlingMethodException {
    throw new NoSuchRequestHandlingMethodException(request);
  }
}

ExceptionHandlerController.java

@ControllerAdvice
public class ExceptionHandlerController extends DefaultHandlerExceptionResolver {
   @ExceptionHandler(NoSuchRequestHandlingMethodException.class)
   public String handleNoSuchMethodtFoundException(NoSuchRequestHandlingMethodException ex){
       return "error/notfound";
    }
}