错误"Could not extract response: no suitable HttpMessageConverter found for response type ... and and



我正在向另一个服务发送一个GET请求,该请求以以下格式返回:

{
"data": [
{
"email": "eskaferas@gmail.com",
"firstName": "Seras",
"lastName": "Meras"
},
{
"email": "Soras@gmail.com",
"firstName": "Oras",
"lastName": "Moras"
},
{
"email": "bzbzb@gmail.com",
"firstName": "hello",
"lastName": "bye"
},
{
"email": "lrc@gmail.com",
"firstName": "Seras",
"lastName": "Meras"
}
],
"message": "Success"
}

我以这种方式使用响应实体:

@GetMapping("/todos/{toDoNoteId}/users")
public ResponseEntity getNotesUsersTest(@PathVariable int toDoNoteId) {
ToDoNote note = toDoNoteService.getToDoNoteById(toDoNoteId);
if(note==null) {
throw new ToDoNoteNotFoundException("Note with id "+ toDoNoteId + " not found");
}
RestTemplate restTemplate = new RestTemplate();
final String uri = "http://friend:5000/users";
try {
ResponseEntity<ArrayResponsePojo> result = restTemplate.exchange(uri, HttpMethod.GET,null, new ParameterizedTypeReference<ArrayResponsePojo>() {}); 
return result;
}
catch (HttpClientErrorException ex) {
return ResponseEntity.status(ex.getRawStatusCode()).headers(ex.getResponseHeaders())
.body(ex.getResponseBodyAsString());
}
}

但是,在发送GET请求时,我收到以下响应: 类型定义错误: [simple type, class com.tdl.model.ArrayResponsePojo];嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance ofcom.tdl.model.ArrayResponsePojo(no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)n at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 2, column: 5]",

我不知道为什么我会得到这个解释,因为我确实有构造器。这是我的pojo类:

public class ArrayResponsePojo {
private User[] data;
private String message;
public User[] getData() {
return data;
}
public void setData(User[] data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@JsonCreator
public ArrayResponsePojo(User[] data, String message) {
this.data = data;
this.message = message;
}
}

还有我的用户类:

public class User {
private String email;
private String firstName;
private String lastName;
@JsonCreator
public User(String email, String firstName, String lastName) {
super();
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}

我尝试了没有参数化类型引用和使用参数化类型引用。 任何人都可以帮忙。我不知道如何解决这个问题。

编辑添加构造函数后,我得到了这个:

"Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdl.model.ArrayResponsePojo] and content type [application/json]".

此问题的所有其他示例都处理与应用程序/json不同的响应类型。

我还尝试将标头设置为应用程序/json。

编辑 2

如果我将响应实体类型设置为字符串,我能够收到响应。但是,我需要收到对我的POJO课程的回复

编辑 3我在调试器中发现了一些警告,指出无法使用 JsonCreators。然后我将 JsonProperty 添加到构造函数参数(或只是空构造函数),现在它不返回错误,而是挂起 30 秒左右,然后才返回预期的结果。 我在这里打开了另一个问题,因为这是一个不同的问题:GET 请求的 Resttemplate 超慢

你的两个类都需要一个 no-arg 构造函数。这就是错误消息告诉您的内容。

最新更新