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.
java spring resttemplate
add a comment |
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.
java spring resttemplate
You receive an array of OpenFigiResponse. And you try to parse it as an object, i.e. a single OpenFigiResponse. That can't work. stackoverflow.com/questions/6349421/…
– JB Nizet
Nov 24 '18 at 23:24
Thank you for your response, it lead me with the answer below to the correct solution.
– Marek W
Nov 25 '18 at 12:44
add a comment |
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.
java spring 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.
java spring resttemplate
java spring resttemplate
edited Nov 25 '18 at 12:37
user7294900
23k113362
23k113362
asked Nov 24 '18 at 23:20
Marek WMarek W
426
426
You receive an array of OpenFigiResponse. And you try to parse it as an object, i.e. a single OpenFigiResponse. That can't work. stackoverflow.com/questions/6349421/…
– JB Nizet
Nov 24 '18 at 23:24
Thank you for your response, it lead me with the answer below to the correct solution.
– Marek W
Nov 25 '18 at 12:44
add a comment |
You receive an array of OpenFigiResponse. And you try to parse it as an object, i.e. a single OpenFigiResponse. That can't work. stackoverflow.com/questions/6349421/…
– JB Nizet
Nov 24 '18 at 23:24
Thank you for your response, it lead me with the answer below to the correct solution.
– Marek W
Nov 25 '18 at 12:44
You receive an array of OpenFigiResponse. And you try to parse it as an object, i.e. a single OpenFigiResponse. That can't work. stackoverflow.com/questions/6349421/…
– JB Nizet
Nov 24 '18 at 23:24
You receive an array of OpenFigiResponse. And you try to parse it as an object, i.e. a single OpenFigiResponse. That can't work. stackoverflow.com/questions/6349421/…
– JB Nizet
Nov 24 '18 at 23:24
Thank you for your response, it lead me with the answer below to the correct solution.
– Marek W
Nov 25 '18 at 12:44
Thank you for your response, it lead me with the answer below to the correct solution.
– Marek W
Nov 25 '18 at 12:44
add a comment |
1 Answer
1
active
oldest
votes
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);
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463237%2fgetting-a-restclientexception-while-trying-to-perform-a-post-request-with-spring%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
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);
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
add a comment |
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);
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
add a comment |
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);
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);
answered Nov 25 '18 at 6:41
user7294900user7294900
23k113362
23k113362
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
add a comment |
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
Hi, I had to tweak the solution, but this was it, thank you!
– Marek W
Nov 25 '18 at 12:34
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463237%2fgetting-a-restclientexception-while-trying-to-perform-a-post-request-with-spring%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You receive an array of OpenFigiResponse. And you try to parse it as an object, i.e. a single OpenFigiResponse. That can't work. stackoverflow.com/questions/6349421/…
– JB Nizet
Nov 24 '18 at 23:24
Thank you for your response, it lead me with the answer below to the correct solution.
– Marek W
Nov 25 '18 at 12:44