Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring:无法反序列化启动\u对象标记之外的实体实例_Java_Json_Spring_Http - Fatal编程技术网

Java Spring:无法反序列化启动\u对象标记之外的实体实例

Java Spring:无法反序列化启动\u对象标记之外的实体实例,java,json,spring,http,Java,Json,Spring,Http,我是Spring新手,我正在尝试测试RESTAPI接口。其中一个端点是“/users”,该端点的GET请求响应(与邮递员一起发送)如下所示: { "_embedded": { "users": [ { "user_id": 77, "username": "test",

我是Spring新手,我正在尝试测试RESTAPI接口。其中一个端点是“/users”,该端点的GET请求响应(与邮递员一起发送)如下所示:

{
    "_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"
        }
    }
}
class Embedded {
  private List<User> users;
}
class Response {
  private Embedded _embedded;
} 

ResponseEntity<User[]> response = restTemplate.getForEntity(
        "/users",
        Response.class
);
用户类如下所示:

@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
如果有人知道问题的原因,我会非常感谢你的帮助。
谢谢大家!

正如您在提供的JSON内容中看到的,方法的响应不是列表或数组。它是一个物体

ResponseEntity<User[]> response = restTemplate.getForEntity(
        "/users",
        User[].class
);
ResponseEntity response=restemplate.getForEntity(
“/用户”,
用户[]。类
);
这是行不通的。您必须创建一个新类,如下所示:

{
    "_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"
        }
    }
}
class Embedded {
  private List<User> users;
}
class Response {
  private Embedded _embedded;
} 

ResponseEntity<User[]> response = restTemplate.getForEntity(
        "/users",
        Response.class
);
嵌入类{
私人名单用户;
}
班级反应{
私有嵌入式(u嵌入式),;
} 
ResponseEntity response=restemplate.getForEntity(
“/用户”,
响应类
);

这是HAL+JSON。非常感谢,这很有帮助,现在它可以正常工作了。