使用apache Velocity进行JSON到JSON转换

问题描述 投票:-1回答: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个回答
0
投票

问题出在这一行:

JSONObject responseobj = new JSONObject(obj.get("response").toString());

你把它作为一个字符串放到JSONObject,这就是它以这种方式工作的原因。

© www.soinside.com 2019 - 2024. All rights reserved.