diff --git a/src/main/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandler.java b/src/main/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandler.java index ff7f691..ebc2242 100644 --- a/src/main/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandler.java +++ b/src/main/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandler.java @@ -2,6 +2,8 @@ import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.DynaBean; +import com.alibaba.fastjson2.JSONObject; +import com.wentong.ladder.aviator.AviatorHelper; import com.wentong.ladder.exceptions.MappingException; import com.wentong.ladder.handler.MappingHandler; import com.wentong.ladder.interceptor.LoggingMappingInterceptor; @@ -56,7 +58,7 @@ public T mapping(S source, T target) { DynaBean dynaBean = DynaBean.create(target); Map sourceMap = BeanUtil.beanToMap(source); Class clazz = target.getClass(); - if (Objects.equals(clazz.getName(), HttpMappingVo.class)) { + if (Objects.equals(clazz.getName(), HttpMappingVo.class.getName())) { MappingFieldWrapper successWrapper = select(clazz.getField("success")); realMapping(source, dynaBean, sourceMap, clazz, successWrapper); boolean success = dynaBean.get("success"); @@ -68,11 +70,9 @@ public T mapping(S source, T target) { realMapping(source, dynaBean, sourceMap, clazz, messageWrapper); } } else { // 处理普通映射 - + Map mappedResponse = (Map) AviatorHelper.COMPILED_FUNCTION.apply(select(HttpMappingVo.class.getField("data")).expression()).execute((JSONObject) source); + dynaBean.set("data", mappedResponse); } - - - T bean = dynaBean.getBean(); mappingInterceptor.afterMapping(source, bean); return bean; diff --git a/src/main/java/com/wentong/ladder/vo/HttpMappingVo.java b/src/main/java/com/wentong/ladder/vo/HttpMappingVo.java index 638c270..ee6bc4d 100644 --- a/src/main/java/com/wentong/ladder/vo/HttpMappingVo.java +++ b/src/main/java/com/wentong/ladder/vo/HttpMappingVo.java @@ -1,10 +1,12 @@ package com.wentong.ladder.vo; +import com.alibaba.fastjson2.JSONObject; + /** * HTTP 映射标准对象 */ -public class HttpMappingVo { +public class HttpMappingVo { public boolean success; public String message; - public T data; + public JSONObject data; } diff --git a/src/test/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandlerTest.java b/src/test/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandlerTest.java index 9c79794..2ef542f 100644 --- a/src/test/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandlerTest.java +++ b/src/test/java/com/wentong/ladder/handler/impl/AntiHttpMappingHandlerTest.java @@ -5,10 +5,12 @@ import com.wentong.ladder.handler.MappingHandler; import com.wentong.ladder.registry.MappingFieldWrapper; import com.wentong.ladder.vo.HttpMappingVo; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import java.util.List; +import java.util.Map; class AntiHttpMappingHandlerTest { @@ -26,7 +28,7 @@ void mapping() { void testMapping() throws Exception { List mappingFieldWrappers = List.of( new MappingFieldWrapper("errorcode == 0", "success判断", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("success")), - new MappingFieldWrapper("data.totalCost", "预估价", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("data")), + new MappingFieldWrapper("let map = seq.map(\"deliveryFee\",decimal(data.totalCost)-decimal(data.defCouCost),\"distance\",data.totalKm); if(decimal(data.defCouCost)!=0) {map[\"discountFee\"]=decimal(data.defCouCost);} return map;", "预估价", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("data")), new MappingFieldWrapper("message", "错误响应", null, MappedType.EXPRESSION, HttpMappingVo.class.getField("message")) ); @@ -51,7 +53,8 @@ void testMapping() throws Exception { JSONObject jsonObject = JSONObject.parseObject(sourceStr); HttpMappingVo mapping = mappingHandler.mapping(jsonObject, HttpMappingVo.class); - System.out.println(mapping); + Assertions.assertTrue(mapping.success); + Assertions.assertEquals("11.19", ((Map) mapping.data.get("data")).get("distance")); } } \ No newline at end of file