[英]How to fetch Self Referencing object in GET call using Spring Data Rest
I am using Spring 1.3.3 and I am unable to get Self Referencing object in the Spring Data Rest Response using GET even if it is not null. 我正在使用Spring 1.3.3,并且即使它不为null,也无法使用GET在Spring Data Rest Response中获取自引用对象。
eg 例如
My Table: 我的桌子:
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `FK_employee_parent` (`parent_id`),
CONSTRAINT `FK_employee_parent` FOREIGN KEY (`parent_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8057 DEFAULT CHARSET=latin1
Response: 响应:
{
"id": 1,
"name": "Test Employee",
"_links": {
"self": {
"href": "http://localhost:8081/employee/1"
},
"employee": {
"href": "http://localhost:8081/employee/1"
},
"parent": {
"href": "http://localhost:8081/employee/1/parent"
}
}
}
But I need the parent_id next to the name field instead of under " links ". 但是我需要名称字段旁边的parent_id,而不是在“ links ”下。
OR 要么
I would suggest to use jackson on top of your spring rest. 我建议在春季休息时使用杰克逊。 You can then easily add an annotation to the domain object to rename links to parent_id.
然后,您可以轻松地将注释添加到域对象,以重命名指向parent_id的链接。
@JsonProperty("parent_id")
You also need to add two annotations above the class 您还需要在类上方添加两个注释
@JsonSerialize
@JsonInclude
Best practice actually is not to use the domain object directly but to use a pojo in between which is dealing with this. 最佳实践实际上不是直接使用域对象,而是在两者之间使用pojo处理该对象。 So then the domain object data will be copied to that pojo and you will only show what you want to show in the rest response.
因此,然后将域对象数据复制到该pojo,您将仅在其余响应中显示要显示的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.