0

I tried to map the json below to Dto but was getting the error: Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.cd.example.api.dtos.PersonDto out of START_ARRAY token. How can I fixed it?

Json

{
    "data": [ 
        {
            "firstname": "Merry",
            "lastname": "Uday",
            "email": "merry@xxx.com"
        },
        {
            "firstname": "Anita",
            "lastname": "Ru",
            "email": "anita@xxx.com"
        }
    ]   
}

PersonDto.java

@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonRootName(value = "data")
public class PersonDto {

    List<PersonDetail> data = Lists.newArrayList();

    @Builder(toBuilder = true)
    @NoArgsConstructor
    @AllArgsConstructor
    @Data
    public static class PersonDetail {
        @JsonProperty("firstname")
        private String firstname;

        @JsonProperty("lastname")
        private String lastname;

        @JsonProperty("email")
        private String email;
    }
}
public <T, K> Mono<T> post(Class<T> respClass, K reqDto, Function<UriBuilder, URI> pathFunction, String token) {

        return webClient
                .post()
                .uri(pathFunction)
                .body(BodyInserters.fromValue(reqDto))
                .header(HttpHeaders.AUTHORIZATION, BEARER + token)
                .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .accept(MediaType.APPLICATION_JSON)
                .acceptCharset(StandardCharsets.UTF_8)
                .exchange()
                .flatMap(clientResponse -> {
                    if (clientResponse.statusCode().is2xxSuccessful()) return clientResponse.bodyToMono(respClass);
                    return clientResponse.createException().flatMap(Mono::error);
                });
    }
bittersour
  • 937
  • 2
  • 11
  • 32

0 Answers0