RestTemplate postForEntity获取DTO列表



我遇到了与这里提到的问题相同的问题。所以我实现了我的代码如下。

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);

这是我使用的DTO。

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

API的预期响应如下。

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

一旦我测试API,它会给我以下错误,我无法理解其原因

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]

有人能帮我解决这个问题吗?

我重现了上述问题。我注意到两件事。

i( 如果响应是案例列表中的对象列表,则没有问题。

ii(如果响应是单个对象,则会出现上述错误。

我建议您研究一下如何在这个API中发送对象:

http://localhost:8080/admin/user/find/all/email"?电子邮件={电子邮件}

p.S我本想把这作为一个评论来补充,但我缺少RP.

最新更新