英文:
Spring Data Rest - Embedded entity not deserialized on POST
问题
我有一个使用Spring Data Rest的项目,其中设置了持久性实体Employer
和Employee
。Employee
与Employer
之间有一个@ManyToOne
关系,定义如下:
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@ManyToOne
private Employer employer;
...
}
当我编写一个单元测试来持久化一个包含Employer
对象的Employee
时,这能够正常工作。然而,当我向我的端点提交带有JSON主体的POST请求时,我注意到反序列化后的Employer
信息为null...
TRACE 16948 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: [org.springframework.data.rest.webmvc.RootResourceInformation@71b498f9, Resource { content: Employee [id=1, ename=John, surname=Snow, jobTitle=Crow, companyName=Wall, companyContact=Warden of North, employeeNumber=, costCentre=, employer=null], links: [] }, org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler@5e618dcb, */*]
这告诉我我的Employee
JSON没有被正确反序列化。我是否需要添加一些配置来自动完成此操作,还是我需要创建自己的反序列化器来处理这种情况?
英文:
I have a spring data rest project set up with Persistence entities Employer
and Employee
. Employee
has a @ManyToOne
relationship defined to Employer
as follows:
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@ManyToOne
private Employer employer;
...
}
This works well when I have a unit test persisting an Employee
that has within it an Employer
object. However, when I submit a POST request with a JSON body to my endpoint, I notice that the deserialized Employer
information is null...
TRACE 16948 --- [nio-8080-exec-1] .w.s.m.m.a.ServletInvocableHandlerMethod : Arguments: [org.springframework.data.rest.webmvc.RootResourceInformation@71b498f9, Resource { content: Employee [id=1, ename=John, surname=Snow, jobTitle=Crow, companyName=Wall, companyContact=Warden of North, employeeNumber=, costCentre=, employer=null], links: [] }, org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler@5e618dcb, */*]
This tells me that my Employee
JSON isn't being deserialized correctly. Is there some config I need to add to get this to happen automatically, or do I need to create my own deserializer to handle this scenario?
答案1
得分: 0
由于仍然没有答案 我按照问题中讨论的方法进行了解决 - 我创建了一个自定义的反序列化程序。
英文:
Seeing as there's still no answer I did the workaround as discussed in the question - I created a custom deserializer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论