[使用Apache Velocity进行JSON到JSON的转换?在我的一个用例中,有没有更好的方法编写模板,下面已经提到?

问题描述 投票:-2回答:1

我正在使用Apache Velocity进行json转换。以下是我的json字符串

    {
    "apiCode": "Payment Execution Service",
    "name": "Initiate a payment",
    "description": "Initiate a payment",
    "request": {
        "method": "POST",
        "path": "/api/v1/payments",
        "headers": [
            {
                "Corporate-ID": "apiKey"
            },
            {
                "Content-Type": "application/json"
            }
        ],
        "body": "{\n  \"beneficiaryInformation\" : {\n    \"destinationAccountIdentifier\" : \"string\",\n    \"destinationBankIdentifier\" : \"DEUTDEDB237\",\n    \"fullName\" : \"JASON SMITH\"\n  },\n  \"purposeOfPayment\" : \"Invoice Payment\",\n  \"remittanceInformation\" : \"Mantainance of Fixtures\",\n  \"remitterInformation\" : {\n    \"sourceAccountCurrency\" : \"EUR\",\n    \"sourceAccountIdentifier\" : \"string\",\n    \"sourceBankIdentifier\" : \"DEUTDEDBFRA\"\n  },\n  \"transferAmount\" : 1.5,\n  \"transferCurrency\" : \"EUR\",\n  \"transferDate\" : \"2015-07-20\",\n  \"transferType\" : \"SCTInst\",\n  \"uniqueRequestNo\" : \"string\"\n}"
    },
    "response": {
        "status": 200,
        "headers": [
            {
                "Content-Type": "application/json"
            }
        ],
        "body": "{\n  \"requestReferenceNo\" : \"string\",\n  \"transactionStatus\" : {\n    \"bankReferenceNo\" : \"string\",\n    \"reasonCode\" : \"string\",\n    \"statusCode\" : \"string\"\n  }\n}"
    },
    "provider": "Payment Execution Service"
}

下面是我的.vm文件

   {
"provider": {
    "name": "$arr[0].apiCode"
},
"consumer": {
    "name": "$arr[0].provider"
},
"interactions": [
#set($i = 0)
#foreach($a in $arr) 
{
        "description": "$a.description",
        "request": {
            "path": "$a.request.path",
            "method": "$a.request.method",
            "headers": $json.valueToString($a.request.headers),
            "body": $json.valueToString($a.request.body)
        },
        "response": {
            "headers": $json.valueToString($a.response.headers),
            "body": $json.valueToString($a.response.body),  
            "status": $a.response.status
        },
        "providerStates": [{
            "name": "$a.description"
        }]
    }
    #set($i = $i + 1)
    #if ($i < $arr.size()), #end
#end
],

"metadata": {
    "pact-jvm": {
        "version": "3.6.3"
    },
    "pactSpecification": {
        "version": "3.0.0"
    }
}

}

下面是我得到的输出

   {
   "provider": {
       "name": "Payment Execution Service"
   },
   "consumer": {
       "name": "Payment Execution Service"
   },
   "interactions": [
       {
           "description": "Initiate a payment",
           "request": {
               "path": "/api/v1/payments",
               "headers": [
                   {
                       "Corporate-ID": "apiKey"
                   },
                   {
                       "Content-Type": "application/json"
                   }
               ],
               "method": "POST",
               "body": "{\n  \"beneficiaryInformation\" : {\n    \"destinationAccountIdentifier\" : \"string\",\n    \"destinationBankIdentifier\" : \"DEUTDEDB237\",\n    \"fullName\" : \"JASON SMITH\"\n  },\n  \"purposeOfPayment\" : \"Invoice Payment\",\n  \"remittanceInformation\" : \"Mantainance of Fixtures\",\n  \"remitterInformation\" : {\n    \"sourceAccountCurrency\" : \"EUR\",\n    \"sourceAccountIdentifier\" : \"string\",\n    \"sourceBankIdentifier\" : \"DEUTDEDBFRA\"\n  },\n  \"transferAmount\" : 1.5,\n  \"transferCurrency\" : \"EUR\",\n  \"transferDate\" : \"2015-07-20\",\n  \"transferType\" : \"SCTInst\",\n  \"uniqueRequestNo\" : \"string\"\n}"
           },
           "response": {
               "headers": [
                   {
                       "Content-Type": "application/json"
                   }
               ],
               "body": "{\n  \"requestReferenceNo\" : \"string\",\n  \"transactionStatus\" : {\n    \"bankReferenceNo\" : \"string\",\n    \"reasonCode\" : \"string\",\n    \"statusCode\" : \"string\"\n  }\n}",
               "status": 200
           },
           "providerStates": [
               {
                   "name": "Initiate a payment"
               }
           ]
       }
    ],
    "metadata": {
        "pact-jvm": {
            "version": "3.6.3"
        },
        "pactSpecification": {
            "version": "3.0.0"
        }
    }
}

符合预期

下面是我尝试过的Java代码

   public String generatePACTUsingVelocity(List<ExchangeRequest> input) {
        /* first, get and initialize an engine */
        VelocityEngine ve = new VelocityEngine();
        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        ve.init();
        /* next, get the Template */
        Template template = ve.getTemplate("spec/pact.vm");

        StringWriter writer = new StringWriter();
        VelocityContext context = new VelocityContext();

        context.put("arr", objectToListHashMap(input));
        context.put("json", JSONStringer.class);

        template.merge(context, writer);
        return writer.toString();

    }

    @SneakyThrows
    private List<HashMap<String, Object>> objectToListHashMap(List<ExchangeRequest> input) {
        List<HashMap<String, Object>> mapList = new ArrayList<HashMap<String, Object>>();
        for (ExchangeRequest req : input) {
            JSONObject obj = new JSONObject(req);
            mapList.add(new ObjectMapper().readValue(obj.toString(), new TypeReference<HashMap<String, Object>>() {
            }));
        }
        return mapList;
    }

还有其他方法可以编写.vm模板以实现相同的输出。.我不想在模板中使用任何Java类,就像我曾经使用过[[JSONStringer.valueToString来将json转换为字符串我希望以这样一种方式来编写模板:任何测试人员都可以编写并将其传递给代码以生成输出。

java velocity
1个回答
1
投票
您输入的是JSONObject的字符串,这就是它如此工作的原因。
© www.soinside.com 2019 - 2024. All rights reserved.