英文:
Deserialize response body with dynamic schema using Spring WebClient
问题
我正在使用Spring WebClient来向外部API发送请求。但是有一个端点在成功的HTTP请求时不返回统一的JSON。当它有数据返回时,我会得到以下JSON响应:
{
"String A": "d7e75c1d-71d1-4628-ad01-cd65a7aabd52",
"String B": "4a28c303-fb5c-4648-926e-fd2db1178838"
}
然而,当没有数据时,它会返回一个空的JSON数组:
[]
在这两种情况下,HTTP请求都以200响应代码完成。
以下是执行请求的代码:
public Map<String, UUID> getIdsByDisplayNames(List<String> displayNames) {
return webClient.get()
.uri(uriBuilder -> {
uriBuilder.path("/api/display-names/account-ids");
for (String displayName : displayNames) {
uriBuilder.queryParam("displayName[]", displayName);
}
return uriBuilder.build();
})
.retrieve()
.bodyToMono(new ParameterizedTypeReference<Map<String, UUID>>() {})
.block();
}
当响应体中有一个JSON对象时,它运行良好,但当端点返回一个空数组时,我会得到一个异常:
org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize value of type
java.util.LinkedHashMap<java.lang.String,java.util.UUID>
from Array value (tokenJsonToken.START_ARRAY
)
我该如何在我的代码中解决这个问题?
英文:
I'm using Spring WebClient to make requests to external API. But there's an endpoint that does not return a uniform json on successful HTTP request. When it has data to return I get a response with following json:
{
"String A": "d7e75c1d-71d1-4628-ad01-cd65a7aabd52",
"String B": "4a28c303-fb5c-4648-926e-fd2db1178838"
}
Howewer when there's nothing it returns an empty json array:
[]
In both cases HTTP request completes with 200 response code.
Here's my code that does a request:
public Map<String, UUID> getIdsByDisplayNames(List<String> displayNames) {
return webClient.get()
.uri(uriBuilder -> {
uriBuilder.path("/api/display-names/account-ids");
for (String displayName : displayNames) {
uriBuilder.queryParam("displayName[]", displayName);
}
return uriBuilder.build();
})
.retrieve()
.bodyToMono(new ParameterizedTypeReference<Map<String, UUID>>() {})
.block();
}
It works well when there's a json object in response body but when endpoint returns an empty array I get an exception:
> org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize value of type java.util.LinkedHashMap<java.lang.String,java.util.UUID>
from Array value (token JsonToken.START_ARRAY
)
How can I solve it in my code?
答案1
得分: 0
代替请求Map<String, UUID>
,您可以请求JsonNode。
该类提供了一些方法isArray
和isObject
,用于确定响应是JSON对象还是JSON数组。
英文:
Instead of asking for a Map<String, UUID>
, you could ask for a JsonNode.
This class provides some methods isArray
and isObject
to determine if the response is a JSON Object or a JSON Array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论