Reputation: 63
I'm integrating with REST API which can return sometimes an object and sometimes an array to the same REST endpoint for example
HTTP 401 with body
{
"code": "_AUTHENTICATION",
"message": "Authentication failure: Unauthenticated.",
"report": false,
"extended": null
}
and
HTTP 200 with body
[
["01K9TP7BQK03104V13CLPA0XD","error accepting record"]
]
My question is how to handle this on client side?
Im using WebClient, java 17 and its a Spring Boot app.
My request is this:
Mono<EventResponse> response = webClient
.post()
.uri(apiUrl)
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(body)
.retrieve()
.bodyToMono(EventResponse.class);
where EventResponse I can map to either object or array returned by this API. For now it looks like this:
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class EventResponse {
private String code;
private String message;
private boolean report;
private String extended;
}
when the API returns an array I have an understable excption
Cannot deserialize value of type `....EventResponse` from Array value...
Upvotes: 1
Views: 36
Reputation: 11
see the @ControllerAdvice https://docs.spring.io/spring-framework/reference/web/webflux/controller/ann-advice.html. In your endpoint you send the valid case and for other problem you throw an error and handle with the controller advice
Upvotes: 1