1

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?

SjAnupa
  • 102
  • 10

1 Answers1

1

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.

Akhil
  • 59
  • 1
  • 11
  • I have attached the response with the problem too. So it is a list – SjAnupa Aug 17 '22 at 10:10
  • Hit the API from the postman and check the response if it's list of objects or a single object. – Akhil Aug 17 '22 at 10:37
  • I have added the response got from postman in the problem – SjAnupa Aug 17 '22 at 10:53
  • Cannot deserialize value of type `java.util.ArrayList` from Object value (token `JsonToken.START_OBJECT`) If you see this error it is clearly stating that it is expecting a List of objects but it got a single object. Can you catch the response using object superclass and verify again? – Akhil Aug 17 '22 at 10:55
  • I have replaced `List` with `Object` class and it returned the last object of the list as the result. But when I tested for same url in postman, it gives me the complete list. I cann't understand where is the problem comes from. – SjAnupa Aug 17 '22 at 11:28
  • So somehow, you are getting a single object when you are making an internal API but you will get a list of objects when you make an API call from the postman.The way you are using restTemplate might be not proper. – Akhil Aug 17 '22 at 12:46
  • 1
    Problem was with the API which returns the data. Thank you – SjAnupa Aug 18 '22 at 17:01