I'm sending a GET request to another service which returns in this format:
{
"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"
}
I'm using a ResponseEntity this way:
@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());
}
}
However when sending the GET request I get this response:
Type definition error: [simple type, class com.tdl.model.ArrayResponsePojo]; nested exception is 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]",
I don't know why I get this exeption since I do have Contructors. Here are is my pojo Class:
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;
}
}
And my user class:
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;
}
}
I tried both without and with the ParameterizedTypeReference. Can anybody please help. I have no clue on how to fix this.
EDIT After adding the constructors I'm getting this instead.:
"Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdl.model.ArrayResponsePojo] and content type [application/json]".
Every other example of this problem was dealing with a different response type than application/json.
I also tried setting the header to application/json.
EDIT 2
If I set the responseEntity type to String I'm able to receive the response. However, I need to receive the responses to my POJO class
EDIT 3 I found a few warnings with the debugger that the JsonCreators could not be used. Then i added JsonProperty to the constructor parameters(or just empty constructors) and now it does not return an error but hangs for 30s or so and only then returns the expected result. I opened another question for that here since it is a different issue:Resttemplate super slow for GET request