簡體   English   中英

無法在 Spring Boot 中反序列化嵌套對象“Role”

[英]Unable to deserialize nested object "Role" in Spring boot

我是 Spring Boot 的新手。 嘗試從員工集合中獲取所有文檔,這些文檔與某些角色相關聯。當嘗試使用 mongo 存儲庫中的“findAll()”方法獲取所有員工文檔時,我得到了空角色,如下面的輸出所示。

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

員工類

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 +
                '}';
    }
}

角色類

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;

    }


}

它沒有正確序列化,因為它沒有實現標記接口。 public class Role更改為public class Role implements Serializable

也不需要@DBRef,它用於將父級和子級作為單獨的文檔與引用一起存儲在 db 中。 向所有類添加序列化,使文檔嵌入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM