Jus*_*tin 10 java spring jackson spring-boot spring-webflux
我在使用Spring反序列化json数组期间遇到问题。我从服务获得此json响应:
[
{
"symbol": "XRPETH",
"orderId": 12122,
"clientOrderId": "xxx",
"price": "0.00000000",
"origQty": "25.00000000",
"executedQty": "25.00000000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "MARKET",
"side": "BUY",
"stopPrice": "0.00000000",
"icebergQty": "0.00000000",
"time": 1514558190255,
"isWorking": true
},
{
"symbol": "XRPETH",
"orderId": 1212,
"clientOrderId": "xxx",
"price": "0.00280000",
"origQty": "24.00000000",
"executedQty": "24.00000000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "SELL",
"stopPrice": "0.00000000",
"icebergQty": "0.00000000",
"time": 1514640491287,
"isWorking": true
},
....
]
Run Code Online (Sandbox Code Playgroud)
我使用Spring WebFlux的新WebClient获取此json,代码如下:
@Override
public Mono<AccountOrderList> getAccountOrders(String symbol) {
return binanceServerTimeApi.getServerTime().flatMap(serverTime -> {
String apiEndpoint = "/api/v3/allOrders?";
String queryParams = "symbol=" +symbol.toUpperCase() + "×tamp=" + serverTime.getServerTime();
String signature = HmacSHA256Signer.sign(queryParams, secret);
String payload = apiEndpoint + queryParams + "&signature="+signature;
log.info("final endpoint:"+ payload);
return this.webClient
.get()
.uri(payload)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(AccountOrderList.class)
.log();
});
}
Run Code Online (Sandbox Code Playgroud)
AccountOrderList
public class AccountOrderList {
private List<AccountOrder> accountOrders;
public AccountOrderList() {
}
public AccountOrderList(List<AccountOrder> accountOrders) {
this.accountOrders = accountOrders;
}
public List<AccountOrder> getAccountOrders() {
return accountOrders;
}
public void setAccountOrders(List<AccountOrder> accountOrders) {
this.accountOrders = accountOrders;
}
}
Run Code Online (Sandbox Code Playgroud)
AccountOrder是映射字段的简单pojo。
实际上,当我点击“ get”时,它会说:
org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize instance of `io.justin.demoreactive.domain.AccountOrder` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `io.justin.demoreactive.domain.AccountOrder` out of START_ARRAY token
at [Source: UNKNOWN; line: -1, column: -1]
Run Code Online (Sandbox Code Playgroud)
如何使用新的webflux模块正确反序列化json?我究竟做错了什么?
更新05/02/2018
两个答案都是正确的。他们完美地解决了我的问题,但最后我决定使用稍微不同的方法:
@Override
public Mono<List<AccountOrder>> getAccountOrders(String symbol) {
return binanceServerTimeApi.getServerTime().flatMap(serverTime -> {
String apiEndpoint = "/api/v3/allOrders?";
String queryParams = "symbol=" +symbol.toUpperCase() + "×tamp=" + serverTime.getServerTime();
String signature = HmacSHA256Signer.sign(queryParams, secret);
String payload = apiEndpoint + queryParams + "&signature="+signature;
log.info("final endpoint:"+ payload);
return this.webClient
.get()
.uri(payload)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToFlux(AccountOrder.class)
.collectList()
.log();
});
}
Run Code Online (Sandbox Code Playgroud)
替代方法是直接返回A Flux,因此您不必将其转换为列表。(这就是通量:n个元素的集合)。
Pin*_*Pin 38
关于您对问题的更新答案,使用bodyToFlux不必要的低效并且在语义上也没有多大意义,因为您并不真正想要订单流。您想要的只是能够将响应解析为列表。
bodyToMono(List<AccountOrder>.class)由于类型擦除而无法工作。您需要能够在运行时保留类型,Spring 提供ParameterizedTypeReference了这一点:
bodyToMono(new ParameterizedTypeReference<List<AccountOrder>>() {})
Run Code Online (Sandbox Code Playgroud)
pvp*_*ran 11
为了使响应与AccountOrderList类匹配,json必须像这样
{
"accountOrders": [
{
"symbol": "XRPETH",
"orderId": 12122,
"clientOrderId": "xxx",
"price": "0.00000000",
"origQty": "25.00000000",
"executedQty": "25.00000000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "MARKET",
"side": "BUY",
"stopPrice": "0.00000000",
"icebergQty": "0.00000000",
"time": 1514558190255,
"isWorking": true
},
{
"symbol": "XRPETH",
"orderId": 1212,
"clientOrderId": "xxx",
"price": "0.00280000",
"origQty": "24.00000000",
"executedQty": "24.00000000",
"status": "FILLED",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "SELL",
"stopPrice": "0.00000000",
"icebergQty": "0.00000000",
"time": 1514640491287,
"isWorking": true
},
....
]
}
Run Code Online (Sandbox Code Playgroud)
这就是错误消息说“ START_ARRAY令牌不足 ”的意思
如果您无法更改响应,请更改代码以接受Array,如下所示
this.webClient.get().uri(payload).accept(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(AccountOrder[].class).log();
Run Code Online (Sandbox Code Playgroud)
您可以将此数组转换为List,然后返回。
Rav*_*avi 10
您的回答很简单List<AccountOrder>。但是,您的 POJO 已将List<AccountOrder>. 所以,根据你的 POJO,你JSON应该是
{
"accountOrders": [
{
Run Code Online (Sandbox Code Playgroud)
但是,你JSON的
[
{
"symbol": "XRPETH",
"orderId": 12122,
....
Run Code Online (Sandbox Code Playgroud)
因此,存在不匹配和反序列化失败。您需要更改为
bodyToMono(AccountOrder[].class)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7131 次 |
| 最近记录: |