尝试使用Springs RestTemplate执行POST请求时获取RestClientException

Marek W:

我想开发一个小功能,使用Springs RestTemplates将ISIN转换为符号。我遇到一个错误,指出以下内容:

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

当我使用String.class作为响应类而不是我的自定义类时,打印响应没有问题OpenFigiResponse

作为参考,API的JSON响应如下所示:

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

我的主要方法抛出错误:

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);

我的回答是:

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;
    }
}

我尝试将消息转换器添加到中,restTemplate但没有实际进展。

user7294900:

您的JSON data在JSON数组中,因此请使用wrapper类OpenFigiListResponse

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

并使用它:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Spring RestTemplate发送XML POST请求?

在执行POST请求并读取用户使用Spring Boot Rest API输入的POST数据时进行JSON模式验证

不推荐使用Springs XmlBeanFactory

使用Python请求模块时尝试/例外

尝试使用springboot从POST请求主体读取嵌套JSON时获取null

尝试使用RestTemplate获取对象列表时响应主体为空

Springs RestTemplate默认连接池

尝试发布AJAX请求时的POST 405(不允许使用方法)-Laravel 4

尝试使用CORS时获取403(禁止)

使用翻新发送POST请求时无法获取POST参数

尝试使用“请求”模块时导入错误

使用获取发出POST请求

使用Google的Text to Speech API一次执行多个请求时,仅获取最后一个请求的音频

尝试使用restTemplate发布到chatbase时为500

在Symfony 4.1上执行JSON请求测试时,“尝试获取非对象的属性”

ErrorException:从Angular 5 Service向Laravel 5.4 API发送POST请求时,“尝试获取非对象的属性”

尝试使用数据进行POST时,Python请求获取TypeError

尝试授权Axios获取请求时出错

尝试POST时无法从请求主体获取对象的值[Node.js,MySQL]

尝试使用网关安全性令牌尝试向Hawkbit服务器获取请求时出现401错误

使用scotty从POST请求执行Shell脚本

为什么在我们尝试获取数据而不更新/提交新数据时,GraphQL为什么仍查询POST请求?

在使用axios的React中尝试将多个参数传递给获取请求时出现415错误

尝试从React前端向Flask后端发出POST请求时获取404

使用XHR执行HTTP POST或PUT请求时,应该检查状态吗?

尝试使用Facebook Graph API访问测试用户时出现“不支持的获取请求”

使用Angular $ resource执行获取请求时出错

使用HttpURLConnection执行POST请求时获取JSON响应

当我尝试调用Outlook Restful API时,获取“只能将操作作为“ POST”请求调用”