Skip to content

Commit

Permalink
feat(echo): String data field of response body support auto convert t…
Browse files Browse the repository at this point in the history
…o specified generic type.
  • Loading branch information
yizzuide committed Oct 13, 2020
1 parent f5d8779 commit 80bef35
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Milkomeda/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<properties>
<java.version>1.8</java.version>
<project.release.version>3.12.2-SNAPSHOT</project.release.version>
<project.release.version>3.12.3-SNAPSHOT</project.release.version>
<spring-boot.version>2.2.4</spring-boot.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<mybatis.starter.version>2.1.1</mybatis.starter.version>
Expand Down Expand Up @@ -67,7 +67,7 @@
<profile>
<id>sonatype-oss-release</id>
<properties>
<project.release.version>3.12.2</project.release.version>
<project.release.version>3.12.3</project.release.version>
</properties>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @author yizzuide
* @since 1.13.0
* @version 3.12.2
* @version 3.12.3
* Create at 2019/09/21 16:48
*/
@Slf4j
Expand Down Expand Up @@ -195,7 +195,7 @@ public <T> EchoResponseData<T> sendRequest(HttpMethod method, String url, Map<St
}
}

EchoResponseData<T> responseData = createReturnData(responseEntity, specType, useStandardHTTP);
EchoResponseData<T> responseData = createReturnData(responseEntity, specType, useStandardHTTP, forceCamel);
if (useStandardHTTP) {
responseData.setCode(String.valueOf(request.getStatusCodeValue()));
}
Expand Down Expand Up @@ -312,14 +312,15 @@ protected boolean useStandardHTTP() {
/**
* 返回数据类型的模板方法
*
* @param <T> EchoResponseData的data字段类型
* @param respData 第三方方响应的数据,Map或List
* @param specType ResponseData的data字段类型
* @param <T> EchoResponseData的data字段类型
* @param useStandardHTTP 是否使用标准的HTTP标准码
* @param forceCamel data里的字段是否强制下划线转驼峰
* @return 统一响应数据类
* @throws EchoException 请求异常
*/
protected abstract <T> EchoResponseData<T> createReturnData(Object respData, TypeReference<T> specType, boolean useStandardHTTP) throws EchoException;
protected abstract <T> EchoResponseData<T> createReturnData(Object respData, TypeReference<T> specType, boolean useStandardHTTP, boolean forceCamel) throws EchoException;

/**
* 子类需要实现的参数签名(默认不应用签名)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
*
* @author yizzuide
* @since 1.13.0
* @version 3.12.2
* @version 3.12.3
* Create at 2019/09/21 19:00
*/
@Slf4j
public abstract class EchoRequest extends AbstractRequest {

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
protected <T> EchoResponseData<T> createReturnData(Object respData, TypeReference<T> specType, boolean useStandardHTTP) throws EchoException {
protected <T> EchoResponseData<T> createReturnData(Object respData, TypeReference<T> specType, boolean useStandardHTTP, boolean forceCamel) throws EchoException {
if (null == respData) {
return responseData();
}
Expand Down Expand Up @@ -98,8 +98,17 @@ protected <T> EchoResponseData<T> createReturnData(Object respData, TypeReferenc
return responseData;
}

// 字符串类型直接返回
if (isStringType && responseData.getData() instanceof String) {
// data内容识别转换
if (responseData.getData() instanceof String) {
// 指定的为字符串类型直接返回
if (isStringType) {
return responseData;
}
if (forceCamel) {
responseData.setData(JSONUtil.toCamel(responseData.getData(), specType));
return responseData;
}
responseData.setData(JSONUtil.nativeRead((String) responseData.getData(), specType));
return responseData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @author yizzuide
* @since 0.2.0
* @version 3.12.2
* @version 3.12.3
* Create at 2019/04/11 22:07
*/
@Slf4j
Expand Down Expand Up @@ -109,12 +109,26 @@ public static <T> T nativeRead(String json, JavaType javaType) {
public static <T> T toCamel(Object data, TypeReference<T> clazz) throws IOException {
ObjectMapper mapper = new ObjectMapper();
Object result = data;
if (data instanceof Map) {
boolean isMap = false;
boolean isList = false;
if (data instanceof String) {
isMap = data.toString().matches("^\\s*\\{.+");
isList = data.toString().matches("^\\s*\\[.+");
if (isMap) {
data = JSONUtil.parseMap((String) data, String.class, Object.class);
} else {
data = JSONUtil.parseList((String) data, Map.class);
}
if (data == null) {
return null;
}
}
if (isMap || data instanceof Map) {
result = toCamel((Map) data);
if (Map.class == TypeUtil.type2Class(clazz)) {
return (T) result;
}
} else if (data instanceof List) {
} else if (isList || data instanceof List) {
List<Map> list = (List) data;
List<Map> targetList = new ArrayList<>();
for (Map m : list) {
Expand Down
2 changes: 1 addition & 1 deletion MilkomedaDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
<milkomeda.version>3.12.2-SNAPSHOT</milkomeda.version>
<milkomeda.version>3.12.3-SNAPSHOT</milkomeda.version>
<mybatis.starter>2.1.1</mybatis.starter>
<redission.version>3.12.5</redission.version>
<zookeeper.version>3.4.14</zookeeper.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.yizzuide.milkomeda.echo.EchoException;
import com.github.yizzuide.milkomeda.echo.EchoRequest;
import com.github.yizzuide.milkomeda.echo.EchoResponseData;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -43,12 +44,7 @@ public ResponseEntity<Map<String, Object>> openAccount(@RequestBody Map<String,
} else {
data.put("code", "200");
data.put("error_msg", "成功");
data.put("data", new HashMap<String, Object>(){
private static final long serialVersionUID = -7494033976315538458L;
{
put("order_id", "12343243434324324");
}});

data.put("data", "{\"order_id\":\"12343243434324324\"}");
}
return ResponseEntity.ok(data);
}
Expand All @@ -66,6 +62,7 @@ public ResponseEntity<Map<String, Object>> requestOpenAccount() {
// TypeReference比用xxx.class在泛型的支持上要强得多,IDE也会智能检测匹配成功
// 如果第三方的data是一个json数组,可以传new TypeReference<List<Map<String, Object>>>() {},返回结果用EchoResponseData<List<Map<String, Object>>>接收
// EchoResponseData<Map<String, Object>> responseData = simpleEchoRequest.sendPostForResult("http://localhost:8091/echo/account/open", reqParams, new TypeReference<Map<String, Object>>() {}, true);
//EchoResponseData<PayVo> responseData = simpleEchoRequest.sendRequest(HttpMethod.POST, "http://localhost:8091/echo/account/open", null, reqParams, new TypeReference<PayVo>(){}, true);
EchoResponseData<Map<String, Object>> responseData = simpleEchoRequest.fetch(HttpMethod.POST, "http://localhost:8091/echo/account/open", reqParams);
log.info("responseData: {}", responseData);
} catch (EchoException e) {
Expand All @@ -78,4 +75,9 @@ public ResponseEntity<Map<String, Object>> requestOpenAccount() {
data.put("error_msg", "");
return ResponseEntity.ok(data);
}

@Data
static class PayVo {
private String orderId;
}
}

0 comments on commit 80bef35

Please sign in to comment.