我在杰克逊序列化方面遇到了一个奇怪的问题 - 我有一个Role
实体有一个嵌套的Permission entity
,而后者又包含一个嵌套的Metadata
实体。当这些实体作为列表从Spring MVC @RestController
中检索时,Jackson将Permission
集合序列化为JSON数组。问题是有时放在这个数组中的元素只是Permission
的id而不是对象的序列化表示。
Role.class:
@Entity
@Table(name = "t_db_roles")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Role.class)
public class Role implements GrantedAuthority {
private final static Logger log = LoggerFactory.getLogger(Permission.class);
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "auto_id")
private int id;
@Column(name = "role", length = 50)
private String name;
@OneToMany(fetch = FetchType.EAGER)
@JoinTable(name = "t_db_role_permissions",
joinColumns = {@JoinColumn(name = "roleid", referencedColumnName = "auto_id")},
inverseJoinColumns = {@JoinColumn(name = "permid", referencedColumnName = "auto_id")}
)
private Set<Permission> permissions;
// getters and setters omitted
}
Permission.class:
@Entity
@Table(name = "t_db_permissions")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Permission.class)
public class Permission implements GrantedAuthority {
private final static Logger log = LoggerFactory.getLogger(Permission.class);
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "auto_id")
private int id;
@Column(name = "name")
private String name;
@OneToOne(mappedBy = "permission")
private Metadata metadata;
}
Metadata.class
@Entity
@Table(name = "t_report_data")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Metadata.class)
public class Metadata {
@Id
@Column(name = "id", insertable = false, updatable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "file_name")
private String fileName;
@Column(name = "human_name")
private String humanName;
@Column(name = "links_to")
@JsonIgnore
private Integer linksTo;
@Column(name = "is_subreport")
@JsonIgnore
private Boolean isSubreport;
@OneToOne(cascade = javax.persistence.CascadeType.ALL, fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "permid")
private Permission permission;
}
控制器:
@RestController
public class RoleRestController {
private final static Logger log = LoggerFactory.getLogger(PermissionRestController.class);
private RoleService roleService;
private MetadataService metadataService;
@Autowired
public void setRoleService(RoleService service) {
this.roleService = service;
}
@Autowired
public void setMetadataService(ReportMetadataService service) { this.metadataService = service; }
@RequestMapping(value = "/admin/roles/", method = RequestMethod.GET)
public List<Role> getRoles() {
return roleService.getRoles();
}
}
我很确定问题出现在序列化中 - 回显List<Role>
到控制台按预期工作,但这里是JSON返回(注意权限数组的第一个元素是整数而不是而不是JSON对象):
{
"id": 10,
"name": "ROLE_TESTER",
"permissions": [
14,
{
"id": 7,
"name": "ViewDailySettlementSummaryGL",
"metadata": {
"id": 41,
"fileName": "acct_summary_gl.rptdesign",
"humanName": "Daily Settlement Summary GL",
"permission": 7
},
"authority": "ViewDailySettlementSummaryGL"
},
{
"id": 6,
"name": "ViewDailySettlementSummary",
"metadata": {
"id": 24,
"fileName": "acct_summary_os.rptdesign",
"humanName": "Daily Settlement Summary",
"permission": 6
},
"authority": "ViewDailySettlementSummary"
}
],
"authority": "ROLE_TESTER"
}
我可以通过手动处理Role
序列化来解决这个问题,但由于SpringMVC / Jackson序列化适用于项目中的其他类,因此我觉得这些类中一定存在问题。有什么想法吗?