0

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 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]",

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

1 Answers1

0

Both of your classes need a no-arg constructor. That's what the error message is telling you.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • After adding the constructors I'm getting this error message: "Could not extract response: no suitable HttpMessageConverter found for response type [class com.tdl.model.ArrayResponsePojo] and content type [application/json]" – Svajunas Kavaliauskas Apr 19 '19 at 11:58
  • You have probably removed Jackson from your classpath since you asked your original question: it tried to deserialize the response, and you got that exception as a result. – JB Nizet Apr 19 '19 at 12:04
  • How would I add it to my classpath? And how could I tell if it is removed? Sorry, if these seem like dumb questions. – Svajunas Kavaliauskas Apr 19 '19 at 12:10
  • I believe I have Jackson set up properly, so something else is not right when getting the response to my custom class. – Svajunas Kavaliauskas Apr 19 '19 at 13:00
  • Use your debugger to see if the MappingJackson2HttpMessageConverter is added when constructing the RestTemplate, and if it's invoked when calling it. – JB Nizet Apr 19 '19 at 13:03
  • Sorry again, If you have time I would need a snipped of a code since I'm a newbie in this. – Svajunas Kavaliauskas Apr 19 '19 at 13:07
  • It's not about having time. It's about identifying why You have that error. I can't send you a snippet of code to let you use your debugger. – JB Nizet Apr 19 '19 at 13:10
  • I just have no Idea on how to check if the MappingJackson2HttpMessageConverter is added, in what part of the code I can access it and check. – Svajunas Kavaliauskas Apr 19 '19 at 13:16
  • In the constructor of RestTemplate. – JB Nizet Apr 19 '19 at 13:16
  • How would I be able to access the constructor? I'm just calling it here: RestTemplate restTemplate = new RestTemplate(); Perhaps we could move to chat or something similiar? I would be very grateful. – Svajunas Kavaliauskas Apr 19 '19 at 13:22
  • Ctrl-Click or Cmd-click on RestTemplate. And there you are: your IDE shows the source code of the RestTempate. Then put a breakpoint inside. Or just put a breakpoint on the line calling the constructor, and then, when the debugger stops at that line use "step inside" to execute the instructions of the constructor one by one. – JB Nizet Apr 19 '19 at 13:24
  • Sadly, I'm running my code on a virtual machine with docker and not with an IDE, I could not move on to and IDE because the services are running on a local docker network. So it only works inside of docker. – Svajunas Kavaliauskas Apr 19 '19 at 13:32
  • If you work in a large-enough company to use a local docker network, then ask help to your co-workers. Not being able to debug your code is not normal at all. You should at least enable remote debugging. – JB Nizet Apr 19 '19 at 13:34
  • I'm not in a company, it is just a requirement for the task I need to do is to have it on a docker network. I have one idea on how to run the debugger I will try and I will get back to you. – Svajunas Kavaliauskas Apr 19 '19 at 13:38
  • I found a few warnings with the debugger that the JsonCreators could not be used. Then i added JsonProperty to the constructor parameters and now it does not return an error but hangs for 30s or so and only then returns the expected result. – Svajunas Kavaliauskas Apr 19 '19 at 14:56