Search code examples
javaspringresttemplate

Getting a RestClientException while trying to perform a POST request with Springs RestTemplate


I want to develop a small feature, which converts an ISIN to a symbol using Springs RestTemplates. I'm running into an error which states the following:

java.lang.IllegalStateException: Failed to execute ApplicationRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:812) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:799) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:341) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at app.Service.main(Service.java:14) [classes/:na]
Caused by: org.springframework.web.client.RestClientException: Error while extracting response for type [class app.model.OpenFigiResponse] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `app.model.OpenFigiResponse` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `app.model.OpenFigiResponse` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:115) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:959) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:942) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:689) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:430) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at app.Service.run(Service.java:37) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:809) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    ... 5 common frames omitted
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `app.model.OpenFigiResponse` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `app.model.OpenFigiResponse` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:243) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:225) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:100) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    ... 12 common frames omitted
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `app.model.OpenFigiResponse` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1138) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1092) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1461) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:185) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013) ~[jackson-databind-2.9.6.jar:2.9.6]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3084) ~[jackson-databind-2.9.6.jar:2.9.6]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:237) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    ... 14 common frames omitted

I have no problem printing the response, when I'm using String.class as the response class instead of my custom class OpenFigiResponse.

For reference, the JSON response from the API looks like this:

[
    {
        "data": [
            {
                [...],
                "name": "APPLE INC",
                "ticker": "AAPL",
                "exchCode": "US",
                [...]
            }
        ]
    }
]

My main method, which throws the error:

final String uri = "https://api.openfigi.com/v1/mapping";

HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);

String requestBody = "[{\n" +
        "\t\"idType\": \"ID_ISIN\",\n" +
        "\t\"idValue\": \"US0378331005\",\n" +
        "\t\"exchCode\":\"US\"\n" +
        "}]";

HttpEntity<String> request = new HttpEntity<>(requestBody, httpHeaders);

RestTemplate restTemplate = new RestTemplate();
HttpEntity<OpenFigiResponse> response = restTemplate.postForEntity(uri, request, OpenFigiResponse.class);

And my response class:

package app.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "data")
public class OpenFigiResponse {
    @JsonProperty("ticker")
    private String ticker;

    public String getTicker() {
        return ticker;
    }

    public void setTicker(String ticker) {
        this.ticker = ticker;
    }
}

I tried adding message convertors to the restTemplate but with no real progress.


Solution

  • Your JSON data is inside a JSON array, so use wrapper class OpenFigiListResponse:

    public class OpenFigiListResponse {
      List<OpenFigiResponse> openFigiResponse;
      //public getter and setter
    }
    

    And use it:

    HttpEntity<OpenFigiResponse> response = restTemplate.postForEntity(uri, request, OpenFigiListResponse.class);