春季数据REST – 嵌入式实体在POST请求中未被反序列化

huangapple go评论66阅读模式
英文:

Spring Data Rest - Embedded entity not deserialized on POST

问题

我有一个使用Spring Data Rest的项目,其中设置了持久性实体EmployerEmployeeEmployeeEmployer之间有一个@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

由于仍然没有答案 春季数据REST – 嵌入式实体在POST请求中未被反序列化 我按照问题中讨论的方法进行了解决 - 我创建了一个自定义的反序列化程序。

英文:

Seeing as there's still no answer 春季数据REST – 嵌入式实体在POST请求中未被反序列化 I did the workaround as discussed in the question - I created a custom deserializer.

huangapple
  • 本文由 发表于 2020年10月20日 23:50:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64448803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定