Twilord
Twilord

Reputation: 47

Spring: Cannot deserialize instance of ENTITY out of START_OBJECT token

I am new to Spring, I am trying to test the REST API interface. One of the endpoints is "/users", and a GET request response for that endpoint (sent with Postman) looks like this:

{
    "_embedded": {
        "users": [
            {
                "user_id": 77,
                "username": "test",
                "password": "$2a$10$NlCTyjTbetNxOvJAMgOzB.ILztgwp1PjG9KoMjIA4I8Oc6Uc9iEGO",
                "enabled": true,
                "money": 0,
                "strategies": [],
                "roles": [
                    {
                        "name": "ROLE_ADMIN",
                        "privileges": [
                            {
                                "name": "FIRST_PRIVILEGE"
                            },
                            {
                                "name": "SECOND_PRIVILEGE"
                            }
                        ]
                    }
                ],
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/users/test"
                    },
                    "users": {
                        "href": "http://localhost:8080/users"
                    },
                    "strategies": {
                        "href": "http://localhost:8080/users/{id}/strategies",
                        "templated": true
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/users"
        }
    }
}

The User class is as follows:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long user_id;

    @Size(min=4, max=12, message="required")
    private String username;

    private String password;
    private boolean enabled;
    private int money;

    //Json Ignore so we dont have a neverending recursion
    @JsonIgnoreProperties("users")
    @ManyToMany(cascade= CascadeType.PERSIST)
    private List<Strategy> strategies = new ArrayList<Strategy>();

    +getters and setters

In my code, I have written a function, that should send a GET request to that endpoint, and get either a List, or an Array of users (I have tried both, they each gave back the same error):

private User[] getAllUsers() {
    ResponseEntity<User[]> response = restTemplate.getForEntity(
            "/users",
            User[].class
    );
    return response.getBody();
}

But I get the following error:

org.springframework.web.client.RestClientException: Error while extracting response for type [class [Lhu.bme.aut.druk.first.domain.User;] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `[Lhu.bme.aut.druk.first.domain.User;` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `[Lhu.bme.aut.druk.first.domain.User;` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]

If anyone has any idea what the cause of the problem could be, I'd really appreciate the help. Thank you!

Upvotes: 0

Views: 636

Answers (1)

Akif Hadziabdic
Akif Hadziabdic

Reputation: 2890

As you can see on the JSON content that you provided, a response from your method is not a list or array. It is an object.

ResponseEntity<User[]> response = restTemplate.getForEntity(
        "/users",
        User[].class
);

This will not work. You have to create a new class like this:

class Embedded {
  private List<User> users;
}
class Response {
  private Embedded _embedded;
} 

ResponseEntity<User[]> response = restTemplate.getForEntity(
        "/users",
        Response.class
);

Upvotes: 1

Related Questions