【问题标题】:ExceptionHandler shared by multiple controllers多个控制器共享的ExceptionHandler
【发布时间】:2011-09-26 15:03:01
【问题描述】:

是否可以在一个类中声明 ExceptionHandlers 并在多个控制器中使用它们,因为在每个控制器中复制粘贴异常处理程序是多余的。

-声明异常处理程序的类:

@ExceptionHandler(IdentifiersNotMatchingException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
def @ResponseBody
String handleIdentifiersNotMatchingException(IdentifiersNotMatchingException e) {
    logger.error("Identifiers Not Matching Error", e)
    return "Identifiers Not Matching Error: " + e.message
}

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
def @ResponseBody
String handleResourceNotFoundException(ResourceNotFoundException e) {
    logger.error("Resource Not Found Error", e)
    return "Resource Not Found Error: " + e.message
}

-联系人控制器

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@RequestMapping(value = "contact/{publicId}", method = RequestMethod.DELETE)
def @ResponseBody
void deleteContact(@PathVariable("publicId") String publicId) throws ResourceNotFoundException, IdentifiersNotMatchingException {...}

-LendingController

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
@RequestMapping(value = "lending/{publicId}", method = RequestMethod.PUT)
def @ResponseBody
void updateLending(@PathVariable("publicId") String publicId, InputStream is) throws ResourceNotFoundException {...}

【问题讨论】:

    标签: java spring spring-mvc


    【解决方案1】:

    做到这一点的一种方法是拥有一个控制器扩展的基类(可以是抽象的)。然后,基类可以保存所有“通用”事物,包括异常处理程序,以及加载通用模型数据,例如用户数据。

    【讨论】:

    • 这个答案有什么问题?当然,它可能不像使用 Spring bean 那样干净,但它仍然是一个可行的选择,并且可能有许多跨控制器的事情是基本控制器可以处理的。
    • 这是我使用的(虽然不是 Spring)
    • 这是否适用于注释。我以为注释没有被继承......
    • @JacobDalton 不,它没有。
    【解决方案2】:

    您可以将HandlerExceptionResolver 声明为将在每个控制器上使用的bean。您只需检查类型并根据需要处理它。

    【讨论】:

    • 酷,我不知道这存在!您是否知道任何示例代码可以显示如何使用的更多详细信息?
    • 这完全取决于您要达到的目标。您可以根据需要实现它,但这里是 Spring 开箱即用的一个简单示例...stackoverflow.com/questions/7405380/…
    • 我必须实现的 resolveException 签名不符合我的需要。我刚刚声明了一个具有异常处理程序的基本抽象类。
    【解决方案3】:

    或者您可以创建一个用于异常处理的接口并将其注入您的控制器类中。

    【讨论】:

      【解决方案4】:

      或者您可以使用带有@ControllerAdvice 注释的类。

      @ControllerAdvice
      public class GlobalExceptionHandler {
      
          @ExceptionHandler(IdentifiersNotMatchingException.class)
          @ResponseStatus(HttpStatus.BAD_REQUEST)
          @ResponseBody
          public String handleIdentifiersNotMatchingException(IdentifiersNotMatchingException e) {
              logger.error("Identifiers Not Matching Error", e)
              return "Identifiers Not Matching Error: " + e.message
          }
      
          @ExceptionHandler(ResourceNotFoundException.class)
          @ResponseStatus(HttpStatus.NOT_FOUND)
          @ResponseBody
          public String handleResourceNotFoundException(ResourceNotFoundException e) {
              logger.error("Resource Not Found Error", e)
              return "Resource Not Found Error: " + e.message
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-13
        • 2016-09-02
        • 1970-01-01
        • 2015-08-02
        • 2012-08-23
        • 2015-02-14
        • 2019-10-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多