I'm receiving JSON from REST API looks like:
{ "items": [ { "id": 60659, "name": "Display", "active": true, "account_id": 235 }, { "id": 36397, "name": " Mail Display", "active": true, "account_id": 107 } ] }
I'm using this method to parse it:
Mono<List<Item>> getItems(String token) { return webCLient .get() .headers(httpHeaders -> httpHeaders.setBearerAuth(token)) .retrieve() .bodyToMono(ItemResponse.class) .map(ItemResponse::getResponse) .retryBackoff(RetrySettings.RETRIES, RetrySettings.FIRST_BACKOFF, RetrySettings.MAX_BACKOFF) .doOnError(e -> log.error("error: " + e.getCause().toString()))
Response:
public class ItemResponse { @JsonProperty("items") private List<Item> response; }
But sometimes 3rd party API returns different response without top level items
property and looks like:
[ { "id": 60659, "name": "Display", "active": true, "account_id": 235 }, { "id": 36397, "name": " Mail Display", "active": true, "account_id": 107 } ]
At this point my app is crashing with JSON decoding error
. I used for this case:
bodyToMono(new ParameterizedTypeReference<List<Item>>() {})
But I can't always refactoring this part of code just to handle their json. How to do it in dynamical way with Spring WebFlux? Like try -> parse#1 -> catch -> parse#2
. So i need to parse json in way#1 and if error occurs app should try to parse it with way#2.
没有评论:
发表评论