JSON不返回相同类型的子对象详细信息

时间:2018-05-30 20:29:12

标签: java rest spring-boot jackson jackson2

我有一个JPA实体,它从REST端点作为ResponseEntity返回。结束点返回该实体的集合 - 下面的代码

public ResponseEntity<Collection<Employee>> getAllEmployees() {
Collection<Employee> returnedEmployees = employeeService.findAll();  
return new ResponseEntity<>(returnedEmployees, HttpStatus.OK);
}

Employee实体的modifiedByUser属性类型为User

当我调用端点时,它返回集合很好但唯一的问题是集合中的第一个元素返回有关modifiedByUser的详细信息,元素的其余部分只是将id映射到它。

样本回复

[
{
"employeeId" : 1,
"firstName" : "John",
"lastName" : "Doe",
"modifiedByUser" : {
   "userId" : 1,
   "firstName" : "Admin"
},

{
"employeeId" : 2,
"firstName" : "Jane",
"lastName" : "Doe",
"modifiedByUser" : 1
}
]

我希望第二个元素中的modifiedByUser属性也显示完整的详细信息。

Employee.java

@Entity
@Table(name = "employee")
@Audited
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "employee_id")
    private int employeeId;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @NotAudited
    @OneToOne
    @JoinColumn(name = "modified_by", referencedColumnName = "user_id", insertable = false, updatable = false)
    private User modifiedByUser;

// getters and setters 

User.java

    @Entity
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
    @EntityListeners(AuditingEntityListener.class)
    @Table(name = "users")
    public class User implements Serializable {

        private static final long serialVersionUID = -74583759743985L;

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "user_id")
        private int userId;

        @Column(name = "first_name")
        private String firstName;
// getters and setters

0 个答案:

没有答案