How can I resolve HttpMessageConverterExtractor error when using exchange method from RestTemplate?

3.3k Views Asked by At

I am using the Spring REST template to get the response from a REST service. I am getting the below exception, but I am unable to figure out the issue. Please find the below details. Am I missing something?

I am getting the correct response when I try this using Postman.

Here AddFileServerBean is the request:

RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(username, password);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<AddFileServerBean> requestEntity = new HttpEntity<AddFileServerBean>(addFileServerBean,headers);
ResponseEntity<ResponseObject> response = restTemplate.exchange(endPointUrl, HttpMethod.POST, requestEntity, ResponseObject.class);

The Exception

Exception in thread "main" java.lang.NoSuchMethodError: org.springframework.util.Assert.noNullElements(Ljava/util/Collection;Ljava/lang/String;)V
at org.springframework.web.client.HttpMessageConverterExtractor.<init>(HttpMessageConverterExtractor.java:77)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.<init>(RestTemplate.java:991)
at org.springframework.web.client.RestTemplate.responseEntityExtractor(RestTemplate.java:822)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:585)
at addfileserver.rest.FileRestTemplate.main(FileRestTemplate.java:280)

Source code for HttpMessageConverterExtractor

@SuppressWarnings("unchecked")
HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverter<?>> messageConverters, Log logger) {
    Assert.notNull(responseType, "'responseType' must not be null");
    Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
    Assert.noNullElements(messageConverters, "'messageConverters' must not contain null elements");
    this.responseType = responseType;
    this.responseClass = (responseType instanceof Class ? (Class<T>) responseType : null);
    this.messageConverters = messageConverters;
    this.logger = logger;
}

My expected Response Object

{
    "dataObj": {
        "responseCode": 0,
        "messageList": [
            "Test"
        ],
        "values": {
            "uniqueId": "Test",
            "pwd": "Test"
        },
        "objectValue": "-"
    },
    "applicationErrors": null
}
1

There are 1 best solutions below

0
On

I think the problem is when you are trying to map the output received from a REST endpoint to ResponseObject.

Also while creating an instance of RestTemplate, try this:

public RestTemplate getRestTemplateInstance(String baseUri) {
    RestTemplate restTemplate = new RestTemplate();
    restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
    restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
    return restTemplate;
}

Get RestTemplate instances by passing baseUri and make a call to your REST endpoint.