How to send body content as raw JSON and not form-data in Spring Boot RestTemplate












0















I have a spring boot application and trying to invoke a rest service of another company by using RestTemplate.



The remote Rest Service required multiple header and body content as Raw JSON.
Here is the sample required body request :



{ 
"amount": "10000",
"destinationNumber": "365412"
}


But my request body generate like this :



{ 
amount= [10000],
destinationNumber= [365412]
}


I've done like this :



    String BASE_URI = "http://server.com/sericeX";
RestTemplate template = new RestTemplate();

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization","Some token");
headers.add("Content-Type", "application/json");

MultiValueMap<String, String> bodyParam = new LinkedMultiValueMap<>();
bodyParam.add("amount", request.getAmount());
bodyParam.add("destinationNumber",request.getDestinationNumber());

HttpEntity entity = new HttpEntity(bodyParam,headers);

ResponseEntity<TransferEntity> responseEntity = template.exchange(BASE_URI, HttpMethod.POST, entity,TransferEntity.class);
TransferEntity transferEntity = responseEntity.getBody();


Could you please tell me how can i generate body request as JSON ?










share|improve this question























  • Try changing this headers.add("Content-Type", "application/json") to headers.add("Content-Type", "text/json");

    – Jignesh M. Khatri
    Nov 13 '18 at 13:46











  • Thanks , I did but no difference.

    – Mostafa
    Nov 13 '18 at 13:49






  • 3





    try to use HashMap instead of MultiValueMap for bodyParam

    – Alex Salauyou
    Nov 13 '18 at 14:06
















0















I have a spring boot application and trying to invoke a rest service of another company by using RestTemplate.



The remote Rest Service required multiple header and body content as Raw JSON.
Here is the sample required body request :



{ 
"amount": "10000",
"destinationNumber": "365412"
}


But my request body generate like this :



{ 
amount= [10000],
destinationNumber= [365412]
}


I've done like this :



    String BASE_URI = "http://server.com/sericeX";
RestTemplate template = new RestTemplate();

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization","Some token");
headers.add("Content-Type", "application/json");

MultiValueMap<String, String> bodyParam = new LinkedMultiValueMap<>();
bodyParam.add("amount", request.getAmount());
bodyParam.add("destinationNumber",request.getDestinationNumber());

HttpEntity entity = new HttpEntity(bodyParam,headers);

ResponseEntity<TransferEntity> responseEntity = template.exchange(BASE_URI, HttpMethod.POST, entity,TransferEntity.class);
TransferEntity transferEntity = responseEntity.getBody();


Could you please tell me how can i generate body request as JSON ?










share|improve this question























  • Try changing this headers.add("Content-Type", "application/json") to headers.add("Content-Type", "text/json");

    – Jignesh M. Khatri
    Nov 13 '18 at 13:46











  • Thanks , I did but no difference.

    – Mostafa
    Nov 13 '18 at 13:49






  • 3





    try to use HashMap instead of MultiValueMap for bodyParam

    – Alex Salauyou
    Nov 13 '18 at 14:06














0












0








0








I have a spring boot application and trying to invoke a rest service of another company by using RestTemplate.



The remote Rest Service required multiple header and body content as Raw JSON.
Here is the sample required body request :



{ 
"amount": "10000",
"destinationNumber": "365412"
}


But my request body generate like this :



{ 
amount= [10000],
destinationNumber= [365412]
}


I've done like this :



    String BASE_URI = "http://server.com/sericeX";
RestTemplate template = new RestTemplate();

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization","Some token");
headers.add("Content-Type", "application/json");

MultiValueMap<String, String> bodyParam = new LinkedMultiValueMap<>();
bodyParam.add("amount", request.getAmount());
bodyParam.add("destinationNumber",request.getDestinationNumber());

HttpEntity entity = new HttpEntity(bodyParam,headers);

ResponseEntity<TransferEntity> responseEntity = template.exchange(BASE_URI, HttpMethod.POST, entity,TransferEntity.class);
TransferEntity transferEntity = responseEntity.getBody();


Could you please tell me how can i generate body request as JSON ?










share|improve this question














I have a spring boot application and trying to invoke a rest service of another company by using RestTemplate.



The remote Rest Service required multiple header and body content as Raw JSON.
Here is the sample required body request :



{ 
"amount": "10000",
"destinationNumber": "365412"
}


But my request body generate like this :



{ 
amount= [10000],
destinationNumber= [365412]
}


I've done like this :



    String BASE_URI = "http://server.com/sericeX";
RestTemplate template = new RestTemplate();

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("Authorization","Some token");
headers.add("Content-Type", "application/json");

MultiValueMap<String, String> bodyParam = new LinkedMultiValueMap<>();
bodyParam.add("amount", request.getAmount());
bodyParam.add("destinationNumber",request.getDestinationNumber());

HttpEntity entity = new HttpEntity(bodyParam,headers);

ResponseEntity<TransferEntity> responseEntity = template.exchange(BASE_URI, HttpMethod.POST, entity,TransferEntity.class);
TransferEntity transferEntity = responseEntity.getBody();


Could you please tell me how can i generate body request as JSON ?







spring spring-boot content-type resttemplate spring-rest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 13:44









MostafaMostafa

1,55963465




1,55963465













  • Try changing this headers.add("Content-Type", "application/json") to headers.add("Content-Type", "text/json");

    – Jignesh M. Khatri
    Nov 13 '18 at 13:46











  • Thanks , I did but no difference.

    – Mostafa
    Nov 13 '18 at 13:49






  • 3





    try to use HashMap instead of MultiValueMap for bodyParam

    – Alex Salauyou
    Nov 13 '18 at 14:06



















  • Try changing this headers.add("Content-Type", "application/json") to headers.add("Content-Type", "text/json");

    – Jignesh M. Khatri
    Nov 13 '18 at 13:46











  • Thanks , I did but no difference.

    – Mostafa
    Nov 13 '18 at 13:49






  • 3





    try to use HashMap instead of MultiValueMap for bodyParam

    – Alex Salauyou
    Nov 13 '18 at 14:06

















Try changing this headers.add("Content-Type", "application/json") to headers.add("Content-Type", "text/json");

– Jignesh M. Khatri
Nov 13 '18 at 13:46





Try changing this headers.add("Content-Type", "application/json") to headers.add("Content-Type", "text/json");

– Jignesh M. Khatri
Nov 13 '18 at 13:46













Thanks , I did but no difference.

– Mostafa
Nov 13 '18 at 13:49





Thanks , I did but no difference.

– Mostafa
Nov 13 '18 at 13:49




3




3





try to use HashMap instead of MultiValueMap for bodyParam

– Alex Salauyou
Nov 13 '18 at 14:06





try to use HashMap instead of MultiValueMap for bodyParam

– Alex Salauyou
Nov 13 '18 at 14:06












1 Answer
1






active

oldest

votes


















0














Thanks to @Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:



HashMap<String, String> bodyParam = new HashMap<>();
bodyParam.put("amount", request.getAmount());
bodyParam.put("destinationNumber",request.getDestinationNumber());





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282396%2fhow-to-send-body-content-as-raw-json-and-not-form-data-in-spring-boot-resttempla%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Thanks to @Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:



    HashMap<String, String> bodyParam = new HashMap<>();
    bodyParam.put("amount", request.getAmount());
    bodyParam.put("destinationNumber",request.getDestinationNumber());





    share|improve this answer




























      0














      Thanks to @Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:



      HashMap<String, String> bodyParam = new HashMap<>();
      bodyParam.put("amount", request.getAmount());
      bodyParam.put("destinationNumber",request.getDestinationNumber());





      share|improve this answer


























        0












        0








        0







        Thanks to @Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:



        HashMap<String, String> bodyParam = new HashMap<>();
        bodyParam.put("amount", request.getAmount());
        bodyParam.put("destinationNumber",request.getDestinationNumber());





        share|improve this answer













        Thanks to @Alex Salauyou based on his comment using HashMap instead of MultiValueMap solved the problem. Here is the changes need to be done:



        HashMap<String, String> bodyParam = new HashMap<>();
        bodyParam.put("amount", request.getAmount());
        bodyParam.put("destinationNumber",request.getDestinationNumber());






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 15:13









        MostafaMostafa

        1,55963465




        1,55963465






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282396%2fhow-to-send-body-content-as-raw-json-and-not-form-data-in-spring-boot-resttempla%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine