Spring mvc 在两个不同的@RestController类中使用两个完全相同的@ExceptionHandler是一种不好的做法吗?

Spring mvc 在两个不同的@RestController类中使用两个完全相同的@ExceptionHandler是一种不好的做法吗?,spring-mvc,exception,spring-restcontroller,Spring Mvc,Exception,Spring Restcontroller,我有两个@RestController类,@RequestMapping(“/persons”)和@RequestMapping(“/person”),它们都可以抛出PersonAccessException,这是一个自定义异常。并由@ExceptionHandler处理 将来还会有更多@RestControllers抛出这个异常,就像在不同的地方一遍又一遍地写同一个方法是不合适的一样,我不确定在不同的rest控制器中复制并粘贴这个完全相同的异常处理程序是否合适 我想知道是否有必要编写一次,然后

我有两个@RestController类,
@RequestMapping(“/persons”)
@RequestMapping(“/person”)
,它们都可以抛出PersonAccessException,这是一个自定义异常。并由@ExceptionHandler处理 将来还会有更多@RestControllers抛出这个异常,就像在不同的地方一遍又一遍地写同一个方法是不合适的一样,我不确定在不同的rest控制器中复制并粘贴这个完全相同的异常处理程序是否合适

我想知道是否有必要编写一次,然后像普通方法一样从不同的类中使用它

在两个不同的@RestController类中使用两个完全相同的@ExceptionHandler是一种不好的做法吗

->这只是避免代码重复和提高可重用性的问题

Spring提供了一种定义全局异常处理程序的方法,该处理程序将应用于应用程序(Web应用程序上下文)中的所有控制器

我们可以使用
@ControllerAdvice
注释来定义处理全局异常的类。用
@ControllerAdvice
注释的类可以显式声明为Springbean,或者通过类路径扫描自动检测

更多信息

我们可以使用
@ExceptionHandle
注释定义特定于异常的异常处理程序(方法)。用
@ExceptionHandle
注释的方法将在多个@Controller类之间共享

更多关于

适用于web应用程序上下文中所有@Controller类的全局异常处理程序示例

    /**
     * <p>This class is to demonstrate global exception handling in spring mvc.</p>
     * <p>This class is declared under *.web package, so it will be detected by dispatcher servlet and will be part of web application context created by dispatcher servlet</p>
     * <br/> It make more sese to declare these classes as a part of web app context and not part of root context because, we do not want these classes to be able to get injected into root context beans.
     * <br/>
     * This class can handle exceptions thrown from <br/>
     *</t> 1. All controllers in application. <br/>
     *</t> 2. All interceptors in applications.
     * 
     *
     */
    @ControllerAdvice // We can us attributes of this annotation to limit which controllers this exception handler advise should apply/advise. If we we do not specify, it will be applied to all controllers in web application context.
    public class GlobalExceptionHandler {

        @ResponseStatus(code = HttpStatus.NOT_FOUND)
        @ExceptionHandler(SpittleNotFoundException.class)
        public ModelAndView handleSpittleNotFoundException(SpittleNotFoundException exception) {
            // all code in this is exactly similar to request handling code in controller
            ModelAndView modelAndView = new ModelAndView("errors/notFound");
            modelAndView.addObject("errorMessage", exception.getMessage());
            return modelAndView;
        }

        @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
        @ExceptionHandler(Throwable.class)
        public String handleGenericException(Throwable exception) {
            return "errors/internalServerError";
        }
    }
/**
*这个类将演示spring mvc中的全局异常处理

*此类在*.web包下声明,因此它将由dispatcher servlet检测,并将成为dispatcher servlet创建的web应用程序上下文的一部分

*
将这些类声明为web应用程序上下文的一部分,而不是根上下文的一部分,这会带来更多的好处,因为我们不希望这些类能够被注入到根上下文bean中。 *
*此类可以处理从
引发的异常 * 1. 应用程序中的所有控制器
* 2. 应用程序中的所有拦截器。 * * */ @ControllerAdvice//我们可以使用此注释的属性来限制此异常处理程序建议应用/建议的控制器。如果不指定,它将应用于web应用程序上下文中的所有控制器。 公共类GlobalExceptionHandler{ @ResponseStatus(代码=HttpStatus.未找到) @ExceptionHandler(SpitleNotFoundException.class) 公共模型和视图句柄PittleNotFoundException(SpitleNotFoundException){ //其中的所有代码与控制器中的请求处理代码完全相似 ModelAndView ModelAndView=新的ModelAndView(“错误/未找到”); modelAndView.addObject(“errorMessage”,exception.getMessage()); 返回模型和视图; } @ResponseStatus(代码=HttpStatus.INTERNAL\u SERVER\u错误) @ExceptionHandler(Throwable.class) 公共字符串HandleGenericeException(可丢弃异常){ 返回“errors/internalServerError”; } }
Spring文档链接