Java Spring@ExceptionHandler未正确处理错误

Java Spring@ExceptionHandler未正确处理错误,java,spring,exception,exception-handling,Java,Spring,Exception,Exception Handling,我有问题,然后我尝试捕捉异常 下面是我实现ResponseErrorHandler的类: public class ErrorGenerator implements ResponseErrorHandler { @Override public void handleError(ClientHttpResponse response) throws IOException { ServiceError error = objectMapper.read

我有问题,然后我尝试捕捉异常

下面是我实现ResponseErrorHandler的类:

    public class ErrorGenerator implements ResponseErrorHandler {

    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        ServiceError error = objectMapper.readValue(response.getBody(), ServiceError.class);

String message = "Test"


    ValidationException exception = new ValidationException(error);

    throw exception;
    }

    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        return true;
    }
}
ValidationException扩展了ServiceException,ServiceException扩展了RuntimeException

这是我的@ControllerAdvice课程

@ExceptionHandler(ServiceException.class)
public ServiceError handleException(ServiceException exception) {
return exception.getError();
}
我收到的错误是:

Exception thrown in handleError: {}

我将非常感谢您的帮助。

需要通过param
HttpServletRequest请求
,并且@Laurynas建议它应该返回
ResponseEntity
。因此,解决方案如下:

    @ExceptionHandler(ServiceException.class)
    public ServiceError handleException(HttpServletRequest request, ServiceException exception) {
         return new ResponseEntity<ServiceError>(exception.getError());
    }
@ExceptionHandler(ServiceException.class)
公共服务错误handleException(HttpServletRequest请求,ServiceException){
返回新的ResponseEntity(exception.getError());
}

这是否编译?ValidationException exception=新的ValidationException(,错误);我认为您需要
HttpServletRequest
作为方法中的参数:
handleException(HttpServletRequest请求,ServiceException)
抱歉,错误:)@保罗:你几乎是对的,我把回程类型改成了ResponseEntity,效果很好。谢谢