Java 使用OAuth2RestTemplate在Rest Api调用中出现异常或错误后继续

Java 使用OAuth2RestTemplate在Rest Api调用中出现异常或错误后继续,java,spring,exception,custom-error-handling,Java,Spring,Exception,Custom Error Handling,我正在使用SpringOAuth2RestTemplate进行rest调用。我试图捕获任何异常,同时尝试进行restAPI调用并继续异常流 我试过两种方法: 方式I(使用try-catch)。 public ResponseEntity<Object> getResponse(URI uri, HttpHeaders httpHeaders, Object obj) { ResponseEntity<Object> response =

我正在使用SpringOAuth2RestTemplate进行rest调用。我试图捕获任何异常,同时尝试进行restAPI调用并继续异常流

我试过两种方法:
方式I(使用try-catch)。

public ResponseEntity<Object> getResponse(URI uri, HttpHeaders httpHeaders,
            Object obj) {

    ResponseEntity<Object> response = null;
    try {
        response = restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity<>(obj, httpHeaders),
                Object.class);

    } catch (Exception serverEx) {
        LOGGER.error("ERROR while calling API.Full Exception: ",serverEx);
        response.getBody().setLink(object.getUrl());
    }

    return response;
}
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {

 private static final Logger LOGGER = LogManager.getLogger(RestTemplateResponseErrorHandler.class);

@Override
public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
    return (httpResponse.getStatusCode().series() == Series.CLIENT_ERROR
            || httpResponse.getStatusCode().series() == Series.SERVER_ERROR);
}

@Override
public void handleError(ClientHttpResponse httpResponse) {
    //Log The Error but contibue the flow
}
}
但这两种方式都无法阻止执行。我想继续执行流程。如果呼叫失败,我想处理它并继续流程。谁能告诉我这里发生了什么事吗

例外情况:

Caused by: java.io.IOException: Attempted read from closed stream.
at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:131)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:118)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at java.io.PushbackInputStream.read(PushbackInputStream.java:139)
at org.springframework.web.client.MessageBodyClientHttpResponseWrapper.hasEmptyMessageBody(MessageBodyClientHttpResponseWrapper.java:102)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:82)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:932)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:916)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:663)
... 223 more
错误响应OutboundJaxrsResponse{status=500,reason=Internal Server Error,hasEntity=true,closed=false,buffered=false}


有什么建议吗?

response.getBody().setLink(object.getUrl())将抛出空指针exception@Deadpool可能是。但执行本身并没有受到阻碍。执行应用程序而不到达catch块。它返回500。是否打印此日志<代码>LOGGER.error(“调用API时出错。完全异常:”,serverEx)@Deadpool No.我没有看到日志:(更新带有错误的问题。将调试点放在
catch
块中并重试,控制器应输入该catch块this
response.getBody().setLink(object.getUrl());
将抛出空指针exception@Deadpool可能是。但是执行本身没有到达catch block。应用程序的执行没有到达catch block。它返回500。此日志是否打印?
LOGGER.error(“调用API时出错。完全异常:”,serverEx);
@Deadpool No。我没有看到日志。:(向上)[注明错误的问题日期。将调试点置于
catch
块并重试,控制器应输入该catch块