我正在尝试编写一个辅助类来直接从 Java Pojo 获取 PactDslJsonBody。但是,它无法创建嵌套的 Json 对象。有人可以解释一下为什么吗?下面是我的代码:
帮助类代码 - 用于将输入的 java pojo 转换为 PactDslJsonBody
public class PactDslHelper {
private static PactDslJsonBody responseBody;
public PactDslJsonBody getResponseBody() {
return responseBody;
}
private PactDslJsonBody updateResponseBody(Class<?> clazz) {
responseBody.object("yooyoo");
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
System.out.println("fieldName is : = " + fieldName);
System.out.println("fieldType Simple Name is : = " + field.getType().getSimpleName());
switch (field.getType().getSimpleName()) {
case "Integer":
responseBody.integerType(fieldName);
break;
case "String":
responseBody.stringType(fieldName);
break;
case "UUID":
responseBody.uuid(fieldName);
break;
case "Boolean":
responseBody.booleanType(fieldName);
break;
}
}
responseBody.closeObject();
return responseBody;
}
public PactDslHelper(Class<?> clazz) {
Field[] fields = clazz.getDeclaredFields();
responseBody = new PactDslJsonBody();
for (Field field : fields) {
Class<?> fieldType = field.getType();
String fieldName = field.getName();
System.out.println("fieldName is : = " + fieldName);
System.out.println("fieldType Simple Name is : = " + field.getType().getSimpleName());
switch (field.getType().getSimpleName()) {
case "Integer":
responseBody.integerType(fieldName);
break;
case "String":
responseBody.stringMatcher(fieldName,ApplicationConstants.REGEX_UUID);
break;
case "UUID":
responseBody.uuid(fieldName);
break;
case "Boolean":
responseBody.booleanType(fieldName);
break;
default:
if (!fieldType.isPrimitive()) {
responseBody = updateResponseBody(fieldType);
}
}
}
}
如何使用这个类的片段:
responseBody_dto = new PactDslHelper(ProductFrontEntity.class);
失败的测试:输入类:
public class ProductFrontEntity {
private String storeId;
private String productId;
private String code;
private ProductIdentificationMap productIdentificationMap;
}
public class ProductIdentificationMap {
private String jAN;
}
预期结果:
{
productId:***,
code:***,
productIdentificationMap{
jan:***
}
}
实际结果:
{
productId:***,
code:***,
jan:***
}