【问题标题】:how to POST Json object to a webservice如何将 Json 对象发布到 Web 服务
【发布时间】:2019-08-01 16:22:23
【问题描述】:

我正在尝试使用 Web 服务并将 JSON 对象作为请求发布到我的程序中。 JSON 是嵌套的。

{
    "paymentorder": {
        "operation": "Purchase",
        "currency": "NOK",
        "amount": 15610,
        "vatAmount": 3122,
        "description": "Test Purchase",
        "userAgent": "Mozilla/5.0...",
        "language": "nb-NO",
        "urls": {
            "hostUrls": ["https://localhost:9002", "https://powertools.local:9002"],
            "completeUrl": "https://powertools.local:9002/payment-completed",
            "cancelUrl": "https://powertools.local:9002/payment-canceled",
            "callbackUrl": "https://powertools.local:9002/payment-callback",
            "termsOfServiceUrl": "https://powertools.local:9002/termsandconditoons.pdf"
        },
        "payeeInfo": {  
            "payeeId": "20f3341c-e570-40a1-b76f-5347f4866de8",
            "payeeReference": "P4555334",
            "payeeName": "Kiran Vemula",
            "productCategory": "P00432101",
            "orderReference" : "P45553234"
        },
        "payer": {
            "consumerProfileRef": "63adb0760ebdcca15d8475773a59c3f3b03df6222dfcc9f5740ce1eb3465f58e"
        }
    }
}

构建哈希图如下:

    private Map<String, Object> initiatePaymentMenuRequestBody(){
        final Map<String, Object> paymentorderChilds = new LinkedHashMap<String, Object>();
        paymentorderChilds.put("operation", "Purchase");
        paymentorderChilds.put("currency",currency);
        paymentorderChilds.put("amount",amount);
        paymentorderChilds.put("vatAmount",vatAmount);
        paymentorderChilds.put("description",description);
        paymentorderChilds.put("userAgent",userAgent);
        paymentorderChilds.put("language",language);
        paymentorderChilds.put("urls", initiatePaymentMenuURLs());
        paymentorderChilds.put("payeeInfo", initiatePaymentMenuPayeeInfo());
        paymentorderChilds.put("payer", initiatePaymentMenuPayer());
        return paymentorderChilds;
    }

    private Map initiatePaymentMenuURLs(){
        final Map<String, Object> initiatePaymentMenuURLs = new LinkedHashMap<String, Object>();
        List<String> hostUrls = new ArrayList<>();
        hostUrls.add(mediqHostUrls1);
        hostUrls.add(mediqHostUrls2);
        initiatePaymentMenuURLs.put("hostUrls",hostUrls);
        initiatePaymentMenuURLs.put("completeUrl",completeUrl);
        initiatePaymentMenuURLs.put("cancelUrl",cancelUrl);
        initiatePaymentMenuURLs.put("callbackUrl",callbackUrl);
        initiatePaymentMenuURLs.put("termsOfServiceUrl",termsOfServiceUrl);

        return initiatePaymentMenuURLs;
    }

    // implement this method with the real data from B2CCustomer and Cart object
    private Map initiatePaymentMenuPayeeInfo(){
        Map<String, String> initiatePaymentMenuPayeeInfo = new LinkedHashMap<String, String>();
        initiatePaymentMenuPayeeInfo.put("payeeId",metchantID);
        initiatePaymentMenuPayeeInfo.put("payeeReference",payeeReference);
        initiatePaymentMenuPayeeInfo.put("payeeName",payeeName);
        initiatePaymentMenuPayeeInfo.put("productCategory",productCategory);
        initiatePaymentMenuPayeeInfo.put("orderReference",orderReference);
        return initiatePaymentMenuPayeeInfo;
    }

    private Map initiatePaymentMenuPayer(){
        Map<String, String> initiatePaymentMenuPayer = new LinkedHashMap<String, String>();
        initiatePaymentMenuPayer.put("consumerProfileRef", initiateConsumerSession());
        return initiatePaymentMenuPayer;
    }

最后调用网络服务发布数据:

    @Override
    public String initiatePaymentMenu(PaymentOrder paymentOrder1) {
        final RestTemplate restTemplate2 = new RestTemplate();
        final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(host+initiatePaymentMenuhostpostfix);
        Map paymentOrder = new HashMap();
        paymentOrder.put("paymentorder", initiatePaymentMenuRequestBody());
        final HttpEntity entity = new HttpEntity(paymentOrder,getHeadders());
        LOG.info("initiatePaymentMenu===========> "+entity.getBody());
        ResponseEntity<String> payExInitiatePaymentMenuResponse = restTemplate2.postForEntity(builder.build().encode().toUri(),entity,String.class);
        LOG.info("initiatePaymentMenu" +payExInitiatePaymentMenuResponse.getStatusCode());
        String returnString = payExInitiatePaymentMenuResponse.getStatusCode().toString();
        return returnString;
    }

我的做法是否正确?我没有得到响应并给我 400 错误。 entity.getBody() 是否打印了准确的 JSON?我可以在邮递员中使用它来检查响应吗?

提前致谢。

【问题讨论】:

  • 我认为您应该使用邮递员(或此类型的任何其他应用程序或浏览器扩展程序)而不是您的应用程序来向该 Web 服务器发送请求。在邮递员中获得正确响应后,然后切换回您的应用程序并对其进行测试。另外,您说服务器给出 404 错误,这意味着请求错误。可能完整的错误标题和正文可以帮助您解决问题。
  • 感谢您的回复。我使用了邮递员,我得到了正确的回应。有什么方法可以检查我的请求的 json。我试图在控制台上打印 entity.getBody(),但是,它与上述预期的 JSON 不完全相同。下面是控制台输出。{ paymentorder={ operation=Purchase, currency=NOK, amount=1235, vatAmount=3122, description=Test Purchase, userAgent=Mozilla 5.0.., language=nb-NO, urls={ hostUrls=[https://electronics.local:9002, https://training.local:9002],

标签: rest spring-mvc hybris


【解决方案1】:

解决了。 Web 服务在 hostUrls 字段中不接受 2 个不同的 URL。这是 Web 服务提供商本身的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多