Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 资源处理程序阻止发生全局异常处理_Java_Spring_Spring Boot_Exception - Fatal编程技术网

Java 资源处理程序阻止发生全局异常处理

Java 资源处理程序阻止发生全局异常处理,java,spring,spring-boot,exception,Java,Spring,Spring Boot,Exception,我正在开发一个SpringBootMVC应用程序,它需要资源处理程序和异常处理来处理不同的情况,比如为控制器输入错误的url或id。在我在配置类中添加资源处理程序之前,我的所有异常方法都能够捕获应用程序中发生的异常,但由于我在配置类中添加了资源处理程序,我用于404和其他异常的handleExceptions方法不再起作用。一旦我在配置类中删除资源处理程序,它就会恢复恢复正常。我在想我怎么才能解决这个问题 异常类 @ControllerAdvice @EnableWebMvc public cl

我正在开发一个SpringBootMVC应用程序,它需要资源处理程序和异常处理来处理不同的情况,比如为控制器输入错误的url或id。在我在配置类中添加资源处理程序之前,我的所有异常方法都能够捕获应用程序中发生的异常,但由于我在配置类中添加了资源处理程序,我用于404和其他异常的
handleExceptions
方法不再起作用。一旦我在配置类中删除资源处理程序,它就会恢复恢复正常。我在想我怎么才能解决这个问题

异常类

@ControllerAdvice
@EnableWebMvc
public class NotFoundExceptionHandler {
    @ExceptionHandler(value = NotFound.class)
    public ModelAndView handError(NotFound notFound) {

        // ModelAndView model = new ModelAndView("404error");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception", notFound.getMessage());
        modelAndView.setViewName("404error");
        Log logger = LogFactory.getLog(NotFoundExceptionHandler.class);
        logger.error(notFound.getMessage() + " " + notFound.fillInStackTrace() + " " + notFound.getCause());


        return modelAndView;
    }
@ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception ex) {

        ModelAndView model = new ModelAndView();
        model.setViewName("404error");
        model.addObject("exception","this is not page for this request" );
        return model;

    }

    @ExceptionHandler(NoHandlerFoundException.class)  //this is a global exception handler which doesn't work
    public ModelAndView handleAllExceptions(Exception ex) {

        ModelAndView model = new ModelAndView();
        model.setViewName("404error");
        model.addObject("exception","this is not page yet created" );
        return model;
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com")
public class ResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
配置类

@ControllerAdvice
@EnableWebMvc
public class NotFoundExceptionHandler {
    @ExceptionHandler(value = NotFound.class)
    public ModelAndView handError(NotFound notFound) {

        // ModelAndView model = new ModelAndView("404error");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception", notFound.getMessage());
        modelAndView.setViewName("404error");
        Log logger = LogFactory.getLog(NotFoundExceptionHandler.class);
        logger.error(notFound.getMessage() + " " + notFound.fillInStackTrace() + " " + notFound.getCause());


        return modelAndView;
    }
@ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception ex) {

        ModelAndView model = new ModelAndView();
        model.setViewName("404error");
        model.addObject("exception","this is not page for this request" );
        return model;

    }

    @ExceptionHandler(NoHandlerFoundException.class)  //this is a global exception handler which doesn't work
    public ModelAndView handleAllExceptions(Exception ex) {

        ModelAndView model = new ModelAndView();
        model.setViewName("404error");
        model.addObject("exception","this is not page yet created" );
        return model;
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com")
public class ResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false
应用程序属性

@ControllerAdvice
@EnableWebMvc
public class NotFoundExceptionHandler {
    @ExceptionHandler(value = NotFound.class)
    public ModelAndView handError(NotFound notFound) {

        // ModelAndView model = new ModelAndView("404error");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("exception", notFound.getMessage());
        modelAndView.setViewName("404error");
        Log logger = LogFactory.getLog(NotFoundExceptionHandler.class);
        logger.error(notFound.getMessage() + " " + notFound.fillInStackTrace() + " " + notFound.getCause());


        return modelAndView;
    }
@ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception ex) {

        ModelAndView model = new ModelAndView();
        model.setViewName("404error");
        model.addObject("exception","this is not page for this request" );
        return model;

    }

    @ExceptionHandler(NoHandlerFoundException.class)  //this is a global exception handler which doesn't work
    public ModelAndView handleAllExceptions(Exception ex) {

        ModelAndView model = new ModelAndView();
        model.setViewName("404error");
        model.addObject("exception","this is not page yet created" );
        return model;
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com")
public class ResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false