简体   繁体   English

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

[英]Returning Custom views for NoSuchRequestHandlingMethodException 404 error in Spring MVC

Whenever user types a wrong url in browser it results in NoSuchRequestHandlingMethodException I want to return a customize view rather than the default view that spring provides with a message. 每当用户在浏览器中NoSuchRequestHandlingMethodException错误的URL时,都会导致NoSuchRequestHandlingMethodException我想返回自定义视图,而不是spring随消息提供的默认视图。 I have @AdviceController in which I tried to catch it through @ExceptionHAndler(NoSuchRequestHandlingMethodException.class) and return a view but it didn't work. 我有@AdviceController ,我试图在其中通过@ExceptionHAndler(NoSuchRequestHandlingMethodException.class)捕获它并返回一个视图,但它不起作用。 I also inherited DefaultHandlerExceptionResolver and overidden the method that handles NoSuchRequestHandlingMethodException , this also didn't work. 我也继承DefaultHandlerExceptionResolver和overidden处理该方法NoSuchRequestHandlingMethodException ,这也没有工作。 I also tried to register view names to exceptions through extending SimpleMappingExceptionResolver setExecptionMapping (in same @AdviceController class) method that also didn't one work. 我还尝试通过扩展SimpleMappingExceptionResolver setExecptionMapping (在同一@AdviceController类中)方法来将视图名称注册为异常, SimpleMappingExceptionResolver方法也没有用。 I don't know what I am doing wrong. 我不知道我在做什么错。 Am I even right about that spring throws NoSuchRequestHandlingMethodException when it doesn't find any mappings in controller Can somebody tell me how to return custom views for Spring Exceptions that are matched with HTTP error codes. 当弹簧在控制器中找不到任何映射时,我什至会抛出NoSuchRequestHandlingMethodException异常有人能告诉我如何为与HTTP错误代码匹配的Spring异常返回自定义视图。

This is my @ControllerAdvice 这是我的@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;
   }
}

UPDATE: Solution 更新:解决方案

The reason that NoSuchRequestHandlingMethodException does not get raised is because Dispatcher Servlet delegates request to default servlet handler(in my case DefaultServletHttpRequestHandler ) and considered it handled. 没有引发NoSuchRequestHandlingMethodException的原因是因为Dispatcher Servlet将请求委托给默认的Servlet处理程序(在我的示例中为DefaultServletHttpRequestHandler )并认为已处理。 The workaround that I have implemented is that I have created a handler method with mapping '/*' that is executed when there is no specific mapping found in any controller and there I through new NoSuchRequestHandlingMethodException which can be caught in @ExceptionHandler and then returned a custom view. 我已经实现的解决方法是,我创建了一个映射为'/ *'的处理程序方法,当在任何控制器中都没有找到特定的映射时执行该方法,并且我通过新的NoSuchRequestHandlingMethodException可以在@ExceptionHandler中捕获该异常,然后返回自定义视图。 However, I do not know the downside of handling exception like this. 但是,我不知道像这样处理异常的缺点。 If any let me know. 如果有的话让我知道。

@Controller
public class HomeController {

...

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

ExceptionHandlerController.java ExceptionHandlerController.java

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

try this way 尝试这种方式

@ControllerAdvice
public class AppWideExceptionHandler {

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

The reason that NoSuchRequestHandlingMethodException does not get raised is because Dispatcher Servlet delegates request to default servlet handler(in my case DefaultServletHttpRequestHandler ) and considered it handled. 没有引发NoSuchRequestHandlingMethodException的原因是因为Dispatcher Servlet将请求委托给默认的Servlet处理程序(在我的示例中为DefaultServletHttpRequestHandler )并认为已处理。 The workaround that I have implemented is that I have created a handler method with mapping '/*' that is executed when there is no specific mapping found in any controller and there I through new NoSuchRequestHandlingMethodException which can be caught in @ExceptionHandler and then returned a custom view. 我已经实现的解决方法是,我创建了一个映射为'/ *'的处理程序方法,当在任何控制器中都没有找到特定的映射时执行该方法,并且我通过新的NoSuchRequestHandlingMethodException可以在@ExceptionHandler中捕获该异常,然后返回自定义视图。 However, I do not know the downside of handling exception like this. 但是,我不知道像这样处理异常的缺点。 If any let me know. 如果有的话让我知道。

@Controller
public class HomeController {

...

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

ExceptionHandlerController.java ExceptionHandlerController.java

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM