gpt4 book ai didi

java - Jackson 反序列化,有 2 个字段自引用同一个类(自引用循环)

转载 作者:行者123 更新时间:2023-12-02 02:25:42 25 4
gpt4 key购买 nike

我有以下类引用自身:

@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
//@JsonIdentityInfo(property="rowId", generator = ObjectIdGenerators.PropertyGenerator.class)
public abstract class AbstractEntity implements Serializable {
/**
*
*/
private static final long serialVersionUID = 568799551343430329L;

@OneToOne(optional=false, fetch=FetchType.EAGER)
@JoinColumn(name="createdBy")
protected User createdBy;
@OneToOne(optional=false, fetch=FetchType.EAGER)
@JoinColumn(name="lastUpdatedBy")
protected User lastUpdatedBy;

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(unique = true, nullable = false, updatable = false, length = 7)
private Integer rowId;

public User getCreatedBy() {
return this.createdBy;
}

public void setCreatedBy(User createdBy) {
this.createdBy = createdBy;
}

public User getLastUpdatedBy() {
return this.lastUpdatedBy;
}

public void setLastUpdatedBy(User lastUpdatedBy) {
this.lastUpdatedBy = lastUpdatedBy;
}

public Integer getRowId() {
return this.rowId;
}

public void setRowId(Integer RowId) {
this.rowId = RowId;
}

public String toString() {
return "[Id]:" + this.rowId + " - [CreatedBy]:" + this.createdBy;
}
}

然后我有一个类User扩展这个类和一个RepositoryUser接口(interface):

public interface RepositoryUser extends CrudRepository<User, Integer> {

}

还有一个 Controller :

@Controller
@RequestMapping(path = "/user")
public class ServiceUser {
@Autowired
private RepositoryUser repositoryUser;

@GetMapping(path="/all", produces = "application/json; charset=UTF-8", headers = "Accept=application/json")
public @ResponseBody Iterable<User> getAllUsers() {
return repositoryUser.findAll();
}

@PostMapping(path="/add", consumes="application/json")
public @ResponseBody User createOneUser(@RequestBody User user) {
System.out.println(user);
return repositoryUser.save(user);
}
}

我的问题是,我在同一个类中两次引用 User (createdby 和 lastupdatedby),并且我尝试了 JsonIdentityInfo、Jsonmanagement、jsonback 都不起作用。正确。

我需要能够拥有{用户 1 数据包括创建者和最后更新者用户 2 数据,包括创建者和最后更新者}

当我添加时,我需要设置创建记录的用户。

你能帮我吗?

非常感谢!

最佳答案

您可以使用 StdSerializer 编写/尝试自定义序列化程序。

CustomJsonSerializer 示例。注意:没有运行代码。

public class CustomJsonSerializer extends StdSerializer<AbstractEntity> {

public CustomJsonSerializer() {
this(null);
}

public CustomJsonSerializer(Class<AbstractEntity> t) {
super(t);
}

@Override
public void serialize(AbstractEntity value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
Field[] fields = value.getClass().getDeclaredFields();
jgen.writeStartObject();

for (Field field : fields) {
field.setAccessible(true);

try {
// Do the proper field mapping for field types . Object type example
jgen.writeObjectField(field.getName(), field.get(value));

} catch (Exception e) {
// catch error
}
}

jgen.writeEndObject();
}
}

然后在您的休息方法上使用@JsonSerialize

@JsonSerialize(using = CustomJsonSerializer.class)
@GetMapping(path="/all", produces = "application/json; charset=UTF-8", headers = "Accept=application/json")
public @ResponseBody Iterable<User> getAllUsers() {
return repositoryUser.findAll();
}

请参阅Custom SerializerStdSerializer

可能的不同解决方案 jackson-bidirectional infinite-recursion

关于java - Jackson 反序列化,有 2 个字段自引用同一个类(自引用循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47808498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com