jsonspring-bootresttemplatespring-resttemplate

RestTemplate postForEntity get List of DTOs


I has the same issue as mentioned in here. So I implemented my code as follows.

String url = "http://localhost:8080/admin/user/find/all/email"+"?email={email}";
ResponseEntity<List<LdapUserDto>> responseEntity = restTemplate.exchange(
                                                                  url,
                                                                  HttpMethod.POST,
                                                                  null,  
                                                                  new ParameterizedTypeReference<List<LdapUserDto>>() {},
                                                                  email);

Here is the DTO I used.

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class LdapUserDto {
    @JsonProperty("id")
    private String id;

    @JsonProperty("email")
    private String email;

    @JsonProperty("fullName")
    private String fullName;

}

The response expected from the API is as follows.

[
    {
        "id": "7264a0e5",
        "email": "Admin@telecom.mu",
        "fullName": "Administrator"
    },
    {
        "id": "0eccd314",
        "email": "ABC.John@telecom.mu",
        "fullName": "ABC John"
    }
]

Once I test the API, it gives me following error which I cannot understand the reason for it.

2022-08-16 01:06:48.407 DEBUG [admin,917e5c4b9e42604c,917e5c4b9e42604c] 15716 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.springframework.web.client.RestClientException: Error while extracting response for type [java.util.List<com.cloudportal.admin.dto.response.user.LdapUserDto>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.ArrayList<com.cloudportal.admin.dto.response.user.LdapUserDto>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<com.cloudportal.admin.dto.response.user.LdapUserDto>` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]
2022-08-16 01:06:48.415 ERROR [admin,917e5c4b9e42604c,917e5c4b9e42604c] 15716 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.RestClientException: Error while extracting response for type [java.util.List<com.cloudportal.admin.dto.response.user.LdapUserDto>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.ArrayList<com.cloudportal.admin.dto.response.user.LdapUserDto>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<com.cloudportal.admin.dto.response.user.LdapUserDto>` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]] with root cause

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<com.cloudportal.admin.dto.response.user.LdapUserDto>` from Object value (token `JsonToken.START_OBJECT`)
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 1]

Can anyone help me to fix this issue?


Solution

  • I reproduced the above problem. I noticed 2 things.

    i) If the response is a list of objects in your case List there is no issue .

    ii) If the response is a single object then you will get the above error.

    I suggest you look into how you are sending the object in this API:

    http://localhost:8080/admin/user/find/all/email"+"?email={email}

    P.S I wouldve added this as a comment but I am short on RP.