问题描述
我有一个使用 RestTemplate 和 ResponseErrorHandle 的消费者 Restful Web 服务,该应用程序生成并使用 json。 由于未知原因,我无法在扩展 ResponseErrorHandler 的类的 handleError() 中抛出我自己的异常。当我尝试调用自定义异常时,我得到以下堆栈跟踪:
ServerRuntime$Responder : An exception mapping did not successfully produce and processed a response. Logging the exception propagated to the container.
com.test.exception.MyOwnServerException:
at com.ing.commonams.exception.RestTemplateResponseErrorHandler.handleError(RestTemplateResponseErrorHandler.java:23) ~[classes/:na]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583) ~[spring-web-5.2.5.RELEASE.jar:5.2.5.RELEASE]
代码如下:
@Component
@Slf4j
public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse httpResponse)
throws IOException {
if (httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) {
//Handle SERVER_ERROR
log.error("Handling Server Error");
if (httpResponse.getStatusCode() == HttpStatus.NOT_IMPLEMENTED) {
throw new MyOwnServerException(httpResponse);
}
} else if (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) {
//Handle CLIENT_ERROR
if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) {
log.error("Handling Client Error");
throw new MyOwnClientException(httpResponse);
}
}
}
一个简单的自定义异常类,
@Getter
public class MyOwnServerException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String applicationId;
private int httpStatusCode;
private String message;
private String detailMessage;
private Long timestamp;
public MyOwnServerException(ClientHttpResponse response) throws IOException {
super();
this.httpStatusCode = response.getRawStatusCode();
this.applicationId = "MS2 Application";
this.message = response.getStatusText();
this.detailMessage = IOUtils.toString(response.getBody(),Charset.defaultCharset());
this.timestamp = System.currentTimeMillis();
}
谁能告诉我这里出了什么问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)