将Java POJO转换为PactDslJsonBody

时间:2018-02-06 07:37:10

标签: java pact pact-jvm

我正在尝试编写一个帮助器类来直接从Java Pojo获取PactDslJsonBody。但是,它无法创建嵌套的Json对象。有人可以解释一下原因吗?下面是我的代码:

HELPER CLASS CODE - 用于将输入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);

测试失败: INPUT 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:***
}

1 个答案:

答案 0 :(得分:0)

可能是因为你自己的代码有问题吗?您似乎没有在对象容器对象内部打开一个新对象,而该对象在最后没有正确关闭,因此为什么一切都是平的。

如果您使用的是java 8,我建议您使用Lambda version of the DSL,因为它更容易理解。

请尽量保持此论坛对Pact特定问题保持开放,而不是围绕您自己的代码无法正常运行的问题。