HOWTO使用使用基于Java的注释配置的Spring MVC全局处理404异常 [英] HOWTO handle 404 exceptions globally using Spring MVC configured using Java based Annotations

查看:114
本文介绍了HOWTO使用使用基于Java的注释配置的Spring MVC全局处理404异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建Spring 4 MVC应用程序.并且它是使用Java注释完全配置的.没有web.xml.像这样,通过使用AbstractAnnotationConfigDispatcherServletInitializerWebMvcConfigurerAdapter的实例配置该应用程序,

I am building a Spring 4 MVC app. And it is completely configured using Java Annotations. There is no web.xml. The app is configured by using instance of AbstractAnnotationConfigDispatcherServletInitializer and WebMvcConfigurerAdapter like so,

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.example.*"})
@EnableTransactionManagement
@PropertySource("/WEB-INF/properties/application.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {
...
}

public class WebAppInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {
...
}

我现在正在尝试为404页添加全局/全部捕获异常处理程序,即HttpStatus.NOT_FOUND,但没有成功.以下是我尝试过的一些方法.

I am now trying to add a global/catch-all exception handler for 404 pages i.e. HttpStatus.NOT_FOUND but no success. Below are some of the ways I tried.

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;

@ControllerAdvice
public class GlobalExceptionHandlerController {

    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ModelAndView handleException (NoSuchRequestHandlingMethodException ex) {
            ModelAndView mav = new ModelAndView();
            return mav;
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ModelAndView handleExceptiond (NoHandlerFoundException ex) {
            ModelAndView mav = new ModelAndView();
            return mav;
    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public void handleConflict() {

    }

    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoSuchRequestHandlingMethodException.class)
    public void handlesdConflict() {
    }

}

这些方法均未执行.我对如何处理这个问题不知所措.我不想使用web.xml,因为那样我就必须为此创建一个.

None of these methods get executed. I am at a loss as to how to handle this. I do not want to use web.xml becasue then I would have to create one just for this.

推荐答案

By default, the DispatcherServlet does not throw a NoHandlerFoundException. You need to enable that.

AbstractAnnotationConfigDispatcherServletInitializer应该让您覆盖DispatcherServlet的创建方式.这样做并致电

The AbstractAnnotationConfigDispatcherServletInitializer should let you override how the DispatcherServlet is created. Do that and call

DispatcherServlet dispatcherServlet = ...; // might get it from super implementation
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

这篇关于HOWTO使用使用基于Java的注释配置的Spring MVC全局处理404异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆