带有VndErrors的@ExceptionHandler不处理错误

时间:2017-03-30 17:58:18

标签: spring-boot error-handling spring-hateoas

问题:在以下示例中,带有void返回类型的处理程序(如果已注释掉)可以正常工作。我得到HttpStatus 422。

如果我切换到使用VndErrors的版本,处理程序将不再起作用,我得到HttpStatus 500,我找不到这种行为的原因。

@ControllerAdvice
@RequestMapping(produces = "application/vnd.error")
@ResponseBody
public class CurrencyCalculatorControllerAdvice {

    // this works
    @ExceptionHandler
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    private void handleCurrencyDetailsNotFoundException(CurrencyDetailsNotFoundException e) {}

    @ExceptionHandler
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    private void handleCurrencyRateNotFoundException(CurrencyRateNotFoundException e) {}

    // this does not work
    @ExceptionHandler
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    VndErrors handleCurrencyDetailsNotFoundException(CurrencyDetailsNotFoundException e) {
        return new VndErrors(e.getLogRef().assemble(), e.getMessage());
    }

    @ExceptionHandler
    @ResponseStatus(HttpStatus.UNPROCESSABLE_ENTITY)
    VndErrors handleCurrencyDetailsNotFoundException(CurrencyRateNotFoundException e) {
        return new VndErrors(e.getLogRef().assemble(), e.getMessage());
    }
}

0 个答案:

没有答案