无法在 Spring Boot 中反序列化嵌套对象“Role”

Posted

技术标签:

【中文标题】无法在 Spring Boot 中反序列化嵌套对象“Role”【英文标题】:Unable to deserialize nested object "Role" in Spring boot 【发布时间】:2019-07-17 09:24:36 【问题描述】:

我是 Spring Boot 的新手。尝试从具有相关角色的员工集合中获取所有文档。当尝试使用“findAll()”方法从 mongo 存储库中获取所有员工文档时,我得到空角色,如下面的输出所示。

Note : Roles are associated with each employee in MongoDB. 

REST 调用的输出

[
    
        "id": 0,
        "name": null,
        "organization": null,
        "email": null,
        "password": null,
        "roles": null,
        "enabled": false,
        "skills": null
    ,
    
        "id": 123,
        "name": "Harry",
        "organization": "Hollywood",
        "email": "harry@demo.com",
        "password": "HarryMovie",
        "roles": [],
        "enabled": true,
        "skills": [
            "Performer",
            "Entertainer",
            "Actor",
            "Producer"
        ]
    ,
    
        "id": 1902,
        "name": "Harry",
        "organization": "Hollywood",
        "email": "harry@demo.com",
        "password": "HarryMovie",
        "roles": [],
        "enabled": true,
        "skills": [
            "Performer",
            "Entertainer",
            "Actor",
            "Producer"
        ]
    
]

Employee.class

package com.app.TestSecurityApp.Pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.List;
import java.util.Set;

@Document(collection="employee")
public class Employee implements Serializable 

    @Id
    private int id;
    private String name;
    private String organization;
    private String email;
    private String password;
    @DBRef
    private List<Role> roles;
    private boolean enabled;
    private List <String> skills;



    public Employee() 
    


    public List <Role> getRoles() 
        return roles;
    

    public void setRoles(List <Role> roles) 
        this.roles = roles;
    

    public boolean isEnabled() 
        return enabled;
    

    public void setEnabled(boolean enabled) 
        this.enabled = enabled;
    

    public String getPassword() 
        return password;
    

    public void setPassword(String password) 
        this.password = password;
    



    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    


    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getorganization() 
        return organization;
    

    public void setorganization(String organization) 
        this.organization = organization;
    

    public List <String> getSkills() 
        return skills;
    

    public void setSkills(List <String> skills) 
        this.skills = skills;
    



    @Override
    public String toString() 
        return "Employee" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", organization='" + organization + '\'' +
                ", email='" + email + '\'' +
                ", password='" + password + '\'' +
                ", roles=" + roles +
                ", enabled=" + enabled +
                ", skills=" + skills +
                '';
    

角色.class

package com.app.TestSecurityApp.Pojo;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.IndexDirection;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;


@Document(collection = "role")
public class Role


    @Id
    private int id;

    @Indexed(unique = true, direction = IndexDirection.DESCENDING, dropDups = true)
    private String role;

    public Role(String role) 
        this.id = id;
        this.role = role;
    

    public Role() 
    

    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    public Role(int id, String role) 
        this.id = id;
        this.role = role;
    

    public String getRole() 
        return role;
    

    public void setRole(String role) 
        this.role = role;
    

    @Override
    public String toString() 
        return "Role" +
                "id=" + id +
                ", role='" + role + '\'' +
                '';
    

弹簧控制器:

package com.app.TestSecurityApp.controllers;


import com.app.TestSecurityApp.Pojo.Employee;
import com.app.TestSecurityApp.Pojo.Role;
import com.app.TestSecurityApp.repository.EmployeeRepsitory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.invoke.MethodType;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/employee")
public class EmployeeAccessControllers 

    @Autowired
    EmployeeRepsitory employeeRepsitory;


    @RequestMapping(value = "/get", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
    public List<Employee> getEmployeeList() 
        return employeeRepsitory.findAll();

    

    @RequestMapping(value = "/set", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public Employee setEmployee(@RequestBody Employee employee) 

        employeeRepsitory.save(employee);
        return employee;

    



【问题讨论】:

【参考方案1】:

它没有正确序列化,因为它没有实现标记接口。将public class Role 更改为public class Role implements Serializable

也不需要@DBRef,它用于将父级和子级作为单独的文档与引用一起存储在数据库中。为所有类添加序列化,使文档嵌入。

【讨论】:

仍然无法让角色反序列化 从 MongoDB 中删除了所有员工文档,然后尝试实现 Serializable 接口,但我仍然可以看到添加的角色,如图所示。 刚刚通过删除“列表角色”上的@DBRef注释重试并且它起作用了。 太棒了 :) 如果你能告诉我何时使用 @DBRef 会很有帮助

以上是关于无法在 Spring Boot 中反序列化嵌套对象“Role”的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 中反序列化多个 LocalDateTime 格式

Jackson 在 Spring Boot 中反序列化 GeoJson Point

在 Spring Boot + Spring Data Rest 中反序列化时忽略带有 @JsonProperty 的字段

在自定义JsonConverter中反序列化嵌套对象List

Spring Boot REST 服务:JSON 反序列化不起作用

在 C# 中反序列化复杂对象