我正在向另一个服务发送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"
}
我以这种方式使用ResponseEntity:
@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请求时,我得到以下响应:
类型定义错误:[简单类型,类com.tdl.model.ArrayResponsePojo];嵌套的异常是com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of
com.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;
}
}
我尝试了不使用ParameterizedTypeReference以及使用ParameterizedTypeReference的情况。 有人可以帮忙吗?我不知道如何解决此问题。
编辑 添加完构造函数后,我得到了它:
"Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdl.model.ArrayResponsePojo] and content type [application/json]".
此问题的每个其他示例都处理的响应类型不同于application / json。
我还尝试将标头设置为application / json。
编辑2
如果将responseEntity类型设置为String,则可以接收到响应。但是,我需要收到对我的POJO类的答复
编辑3 我在调试器中发现了一些警告,提示无法使用JsonCreators。然后我将JsonProperty添加到构造函数参数(或只是空的构造函数),现在它不返回错误,但挂起30秒钟左右,然后才返回预期结果。 我在这里提出了另一个问题,因为这是一个不同的问题:Resttemplate super slow for GET request
答案 0 :(得分:0)
两个类都需要一个无参数的构造函数。这就是错误消息告诉您的内容。