feat: 升级1.0.9,解决异常情况
This commit is contained in:
parent
a814b9e598
commit
b60caa6bd2
4
pom.xml
4
pom.xml
@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.0.8</version>
|
<version>1.0.9</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>8</maven.compiler.source>
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
@ -22,7 +22,7 @@
|
|||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
<commons-collection.version>4.4</commons-collection.version>
|
<commons-collection.version>4.4</commons-collection.version>
|
||||||
<commons.lang.version>2.6</commons.lang.version>
|
<commons.lang.version>2.6</commons.lang.version>
|
||||||
<sdk.version>1.0.8</sdk.version>
|
<sdk.version>1.0.9</sdk.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.0.8</version>
|
<version>1.0.9</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<groupId>group.flyfish</groupId>
|
<groupId>group.flyfish</groupId>
|
||||||
<artifactId>rest-proxy</artifactId>
|
<artifactId>rest-proxy</artifactId>
|
||||||
<version>1.0.8</version>
|
<version>1.0.9</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
@ -214,4 +214,10 @@ public interface RestConstants {
|
|||||||
.with(TypeConstants.BYTE_ARRAY, ResponseType.BINARY)
|
.with(TypeConstants.BYTE_ARRAY, ResponseType.BINARY)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
// 提示消息
|
||||||
|
String MSG_THREAD_POOL_EMPTY = "线程池未指定或为空!";
|
||||||
|
String MSG_IO_ERROR = "发起请求时出现异常!";
|
||||||
|
String MSG_UNKNOWN_HOST = "未知的请求地址!";
|
||||||
|
String MSG_REQUEST_ERROR = "请求接口{0}状态异常!代码:{1}";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -40,12 +40,11 @@ import static group.flyfish.rest.constants.RestConstants.DEFAULT_EXECUTOR;
|
|||||||
* 6. 新增单例httpClient模式,复用连接让应用更高效
|
* 6. 新增单例httpClient模式,复用连接让应用更高效
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
final class DefaultRestClient implements RestClient {
|
final class DefaultRestClient extends RestErrorHandler implements RestClient {
|
||||||
|
|
||||||
private final HttpRequestBase request;
|
private final HttpRequestBase request;
|
||||||
private boolean async = false;
|
private boolean async = false;
|
||||||
private Consumer<HttpEntity> consumer;
|
private Consumer<HttpEntity> consumer;
|
||||||
private Consumer<RestClientException> errorConsumer;
|
|
||||||
private ExecutorService executorService;
|
private ExecutorService executorService;
|
||||||
private ResponseType responseType = ResponseType.NORMAL;
|
private ResponseType responseType = ResponseType.NORMAL;
|
||||||
private Class<?> resultClass;
|
private Class<?> resultClass;
|
||||||
@ -85,18 +84,6 @@ final class DefaultRestClient implements RestClient {
|
|||||||
return this;
|
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;
|
this.consumer = consumer;
|
||||||
if (this.async) {
|
if (this.async) {
|
||||||
if (this.executorService == null) {
|
if (this.executorService == null) {
|
||||||
handleError(new RestClientException("线程池未指定或为空!"));
|
handleError(RestConstants.MSG_THREAD_POOL_EMPTY);
|
||||||
}
|
}
|
||||||
this.executorService.submit(this::executeSafety);
|
this.executorService.submit(this::executeSafety);
|
||||||
} else {
|
} else {
|
||||||
@ -202,7 +189,7 @@ final class DefaultRestClient implements RestClient {
|
|||||||
try {
|
try {
|
||||||
execute();
|
execute();
|
||||||
} catch (IOException e) {
|
} 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());
|
log.info("【Rest Invoke】{} {}", request.getMethod(), request.getURI());
|
||||||
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
|
try (CloseableHttpResponse response = clientProvider.getClient().execute(request)) {
|
||||||
StatusLine statusLine = response.getStatusLine();
|
StatusLine statusLine = response.getStatusLine();
|
||||||
if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {
|
|
||||||
int requestCode = statusLine.getStatusCode();
|
|
||||||
log.error(request.getURI() + "接口调用失败,code:" + requestCode);
|
|
||||||
handleError(new RestClientException("网络请求状态异常!代码:" + requestCode, null, statusLine));
|
|
||||||
} else {
|
|
||||||
HttpEntity entity = response.getEntity();
|
HttpEntity entity = response.getEntity();
|
||||||
if (consumer != null) {
|
if (HttpStatus.valueOf(statusLine.getStatusCode()).isError()) {
|
||||||
consumer.accept(entity);
|
handleError(request.getURI(), statusLine.getStatusCode(), handleEntity(entity));
|
||||||
return null;
|
} else {
|
||||||
}
|
return handleEntity(entity);
|
||||||
return resolveResponse(entity);
|
|
||||||
}
|
}
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException e) {
|
||||||
handleError(new RestClientException(e.getMessage(), e, null));
|
handleError(RestConstants.MSG_UNKNOWN_HOST, e);
|
||||||
log.error("未知的请求地址!");
|
|
||||||
} finally {
|
} finally {
|
||||||
request.releaseConnection();
|
request.releaseConnection();
|
||||||
}
|
}
|
||||||
@ -318,6 +298,24 @@ final class DefaultRestClient implements RestClient {
|
|||||||
return RestConstants.RESPONSE_TYPE_MAP.getOrDefault(clazz.getName(), ResponseType.OBJECT);
|
return RestConstants.RESPONSE_TYPE_MAP.getOrDefault(clazz.getName(), ResponseType.OBJECT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理响应体
|
||||||
|
*
|
||||||
|
* @param entity 响应体
|
||||||
|
* @return 结果
|
||||||
|
* @throws IOException 异常
|
||||||
|
*/
|
||||||
|
private <T> T handleEntity(HttpEntity entity) throws IOException {
|
||||||
|
if (null == entity) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (consumer != null) {
|
||||||
|
consumer.accept(entity);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return resolveResponse(entity);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解析结果
|
* 解析结果
|
||||||
*
|
*
|
||||||
|
@ -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<RestClientException> 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));
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,19 @@
|
|||||||
package group.flyfish.rest.core.exception;
|
package group.flyfish.rest.core.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常类,用于包装异常
|
* 异常类,用于包装异常
|
||||||
*/
|
*/
|
||||||
public class RestClientException extends RuntimeException {
|
public class RestClientException extends RuntimeException {
|
||||||
|
|
||||||
private static final long serialVersionUID = 4741281547788724661L;
|
private static final long serialVersionUID = 4741281547788724661L;
|
||||||
|
@Getter
|
||||||
private Exception nested;
|
private Exception nested;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private int statusCode;
|
||||||
|
|
||||||
private Object bind;
|
private Object bind;
|
||||||
|
|
||||||
public RestClientException(String message, Exception nested) {
|
public RestClientException(String message, Exception nested) {
|
||||||
@ -15,9 +21,9 @@ public class RestClientException extends RuntimeException {
|
|||||||
this.nested = nested;
|
this.nested = nested;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RestClientException(String message, Exception nested, Object bind) {
|
public RestClientException(String message, int statusCode, Object bind) {
|
||||||
super(message);
|
super(message);
|
||||||
this.nested = nested;
|
this.statusCode = statusCode;
|
||||||
this.bind = bind;
|
this.bind = bind;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,12 +35,4 @@ public class RestClientException extends RuntimeException {
|
|||||||
public <T> T getBind() {
|
public <T> T getBind() {
|
||||||
return (T) bind;
|
return (T) bind;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBind(Object bind) {
|
|
||||||
this.bind = bind;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Exception getNested() {
|
|
||||||
return nested;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user