1
votes

I have to write a procedure which will accept body in Hashmap format. I have created HttpEntity with Hashmap values & headers.

public <T> void doPOSTRequest(String url, T body, HttpHeaders headers) throws JsonProcessingException {

    HttpEntity<T> request = new HttpEntity<T>(body,headers);
    System.out.println("Printing Request :" + request);
    ResponseEntity<String> response = null;

    //Calling POST Method
    //response=restTemplate.postForObject(url,request,String.class);
     response=restTemplate.exchange(url, HttpMethod.POST,request,String.class);


    System.out.println(response);

}

I am facing below exception :

Exception in thread "main" org.springframework.web.client.RestClientException: No HttpMessageConverter for [java.util.HashMap] at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:957) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:733) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:579) at Com.RESTRequest.doPOSTRequest(RESTRequest.java:39) at Com.GenericREST.main(GenericREST.java:30)

1

1 Answers

0
votes

Try using MultiValueMap instead of Generic like this

HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(parameters, headers);

Full block:

public <T> void doPOSTRequest(String url, T body, HttpHeaders headers) throws JsonProcessingException {
    HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(body,headers);
    System.out.println("Printing Request :" + request);
    ResponseEntity<String> response = null;
    //Calling POST Method
    //response=restTemplate.postForObject(url,request,String.class);
     response=restTemplate.exchange(url, HttpMethod.POST,request,String.class);

    System.out.println(response);
}