【问题标题】:REST send ManyToMany back to ServerREST 将 ManyToMany 发送回服务器
【发布时间】:2016-02-08 18:17:10
【问题描述】:

我有一个@ManyToMany 和一个@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)

如果我将对象发回,我会得到:

error: "Bad Request"
exception: "org.springframework.http.converter.HttpMessageNotReadableException"
message: "Could not read document: No _valueDeserializer assigned↵ at [Source: java.io.PushbackInputStream@3021b8dc; line: 1, column: 244] (through reference chain: at.scool.model.db.Teacher["classes"]->java.util.HashSet[0]->at.scool.model.db.SchoolClass["teachers"]->java.util.HashSet[0]->at.scool.model.db.Teacher["id"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No _valueDeserializer assigned↵ at [Source: java.io.PushbackInputStream@3021b8dc; line: 1, column: 244] (through reference chain: at.scool.model.db.Teacher["classes"]->java.util.HashSet[0]->at.scool.model.db.SchoolClass["teachers"]->java.util.HashSet[0]->at.scool.model.db.Teacher["id"])"
path: "/api/teacher/1"
status: 400
timestamp: 1446921619542

源代码

Spring boot with JPA and ManyToMany: JsonIdentifyInfo not working properly

老师

@ManyToMany
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
private Set<SchoolClass> classes = new HashSet<>();

学校班级

@ManyToMany
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
private Set<Teacher> teachers = new HashSet<>();

【问题讨论】:

  • 你确定你的语法正确吗??
  • 用我的代码更新帖子
  • @rala 你得到解决方案了吗?

标签: java spring rest spring-boot


【解决方案1】:

@zt1983811
是的 - 我找到了一个我忘记在这里发布的解决方案
一个原因:我不这么认为程序“很好”,但我们开始吧:

回答

一个班级需要@JsonIdentityInfo在顶部:

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
public class Teacher {
// ...
@ManyToOne(optional = false)
@JsonBackReference(value = "teachers")
private School school;

另一个需要它的变量:

@ManyToMany
@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
private Set<Teacher> teachers = new HashSet<>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2016-01-30
    • 2019-02-08
    相关资源
    最近更新 更多