diff --git a/pom.xml b/pom.xml index a54565d..b51bc3e 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ group.flyfish rest-proxy - 1.0.8 + 1.0.9 8 @@ -22,7 +22,7 @@ 1.8 4.4 2.6 - 1.0.8 + 1.0.9 pom diff --git a/rest-proxy-api/pom.xml b/rest-proxy-api/pom.xml index 137b3e7..0356bc1 100644 --- a/rest-proxy-api/pom.xml +++ b/rest-proxy-api/pom.xml @@ -5,7 +5,7 @@ group.flyfish rest-proxy - 1.0.8 + 1.0.9 4.0.0 diff --git a/rest-proxy-core/pom.xml b/rest-proxy-core/pom.xml index 9481871..1447af5 100644 --- a/rest-proxy-core/pom.xml +++ b/rest-proxy-core/pom.xml @@ -5,7 +5,7 @@ group.flyfish rest-proxy - 1.0.8 + 1.0.9 4.0.0 diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java b/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java index dba8d41..ee9c914 100644 --- a/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java +++ b/rest-proxy-core/src/main/java/group/flyfish/rest/constants/RestConstants.java @@ -214,4 +214,10 @@ public interface RestConstants { .with(TypeConstants.BYTE_ARRAY, ResponseType.BINARY) .build(); + // 提示消息 + String MSG_THREAD_POOL_EMPTY = "线程池未指定或为空!"; + String MSG_IO_ERROR = "发起请求时出现异常!"; + String MSG_UNKNOWN_HOST = "未知的请求地址!"; + String MSG_REQUEST_ERROR = "请求接口{0}状态异常!代码:{1}"; + } diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java b/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java index 80a41c6..58ed0d8 100644 --- a/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java +++ b/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/DefaultRestClient.java @@ -40,12 +40,11 @@ import static group.flyfish.rest.constants.RestConstants.DEFAULT_EXECUTOR; * 6. 新增单例httpClient模式,复用连接让应用更高效 */ @Slf4j -final class DefaultRestClient implements RestClient { +final class DefaultRestClient extends RestErrorHandler implements RestClient { private final HttpRequestBase request; private boolean async = false; private Consumer consumer; - private Consumer errorConsumer; private ExecutorService executorService; private ResponseType responseType = ResponseType.NORMAL; private Class resultClass; @@ -85,18 +84,6 @@ final class DefaultRestClient implements RestClient { return this; } - /** - * 错误处理 - * - * @param e 异常 - */ - private void handleError(RestClientException e) { - if (null != errorConsumer) { - errorConsumer.accept(e); - } else { - throw e; - } - } /** * 设置响应类型 @@ -155,7 +142,7 @@ final class DefaultRestClient implements RestClient { this.consumer = consumer; if (this.async) { if (this.executorService == null) { - handleError(new RestClientException("线程池未指定或为空!")); + handleError(RestConstants.MSG_THREAD_POOL_EMPTY); } this.executorService.submit(this::executeSafety); } else { @@ -202,7 +189,7 @@ final class DefaultRestClient implements RestClient { try { execute(); } catch (IOException e) { - handleError(new RestClientException(e.getMessage(), e, null)); + handleError(RestConstants.MSG_IO_ERROR, e); } } @@ -287,21 +274,14 @@ final class DefaultRestClient implements RestClient { log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI()); try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) { StatusLine statusLine = response.getStatusLine(); + HttpEntity entity = response.getEntity(); if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) { - int requestCode = statusLine.getStatusCode(); - log.error(request.getURI() + "接口调用失败,code:" + requestCode); - handleError(new RestClientException("网络请求状态异常!代码:" + requestCode, null, statusLine)); + handleError(request.getURI(), statusLine.getStatusCode(), handleEntity(entity)); } else { - HttpEntity entity = response.getEntity(); - if (consumer != null) { - consumer.accept(entity); - return null; - } - return resolveResponse(entity); + return handleEntity(entity); } } catch (UnknownHostException e) { - handleError(new RestClientException(e.getMessage(), e, null)); - log.error("未知的请求地址!"); + handleError(RestConstants.MSG_UNKNOWN_HOST, e); } finally { request.releaseConnection(); } @@ -318,6 +298,24 @@ final class DefaultRestClient implements RestClient { return RestConstants.RESPONSE_TYPE_MAP.getOrDefault(clazz.getName(), ResponseType.OBJECT); } + /** + * 处理响应体 + * + * @param entity 响应体 + * @return 结果 + * @throws IOException 异常 + */ + private T handleEntity(HttpEntity entity) throws IOException { + if (null == entity) { + return null; + } + if (consumer != null) { + consumer.accept(entity); + return null; + } + return resolveResponse(entity); + } + /** * 解析结果 * diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/RestErrorHandler.java b/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/RestErrorHandler.java new file mode 100644 index 0000000..cf2ac6e --- /dev/null +++ b/rest-proxy-core/src/main/java/group/flyfish/rest/core/client/RestErrorHandler.java @@ -0,0 +1,64 @@ +package group.flyfish.rest.core.client; + +import group.flyfish.rest.constants.RestConstants; +import group.flyfish.rest.core.exception.RestClientException; +import lombok.extern.slf4j.Slf4j; + +import java.net.URI; +import java.text.MessageFormat; +import java.util.function.Consumer; + +/** + * rest请求错误处理器 + * + * @author wangyu + */ +@Slf4j +public abstract class RestErrorHandler { + + protected Consumer errorConsumer; + + /** + * 错误处理 + * + * @param e 异常 + */ + private void handleError(RestClientException e) { + log.error(e.getMessage()); + if (null != errorConsumer) { + errorConsumer.accept(e); + } else { + throw e; + } + } + + /** + * 处理常规文本异常 + * + * @param message 消息 + */ + protected void handleError(String message) { + handleError(new RestClientException(message)); + } + + /** + * 处理具体发生的异常 + * + * @param message 信息 + * @param cause 造成的异常 + */ + protected void handleError(String message, Exception cause) { + handleError(new RestClientException(message + cause.getMessage(), cause)); + } + + /** + * 处理请求异常 + * + * @param status 状态码 + * @param data 响应数据 + */ + protected void handleError(URI uri, int status, Object data) { + String message = MessageFormat.format(RestConstants.MSG_REQUEST_ERROR, uri, status); + handleError(new RestClientException(message, status, data)); + } +} diff --git a/rest-proxy-core/src/main/java/group/flyfish/rest/core/exception/RestClientException.java b/rest-proxy-core/src/main/java/group/flyfish/rest/core/exception/RestClientException.java index 9e1aa56..c5a5469 100644 --- a/rest-proxy-core/src/main/java/group/flyfish/rest/core/exception/RestClientException.java +++ b/rest-proxy-core/src/main/java/group/flyfish/rest/core/exception/RestClientException.java @@ -1,13 +1,19 @@ package group.flyfish.rest.core.exception; +import lombok.Getter; + /** * 异常类,用于包装异常 */ public class RestClientException extends RuntimeException { private static final long serialVersionUID = 4741281547788724661L; + @Getter private Exception nested; + @Getter + private int statusCode; + private Object bind; public RestClientException(String message, Exception nested) { @@ -15,9 +21,9 @@ public class RestClientException extends RuntimeException { this.nested = nested; } - public RestClientException(String message, Exception nested, Object bind) { + public RestClientException(String message, int statusCode, Object bind) { super(message); - this.nested = nested; + this.statusCode = statusCode; this.bind = bind; } @@ -29,12 +35,4 @@ public class RestClientException extends RuntimeException { public T getBind() { return (T) bind; } - - public void setBind(Object bind) { - this.bind = bind; - } - - public Exception getNested() { - return nested; - } }