gpt4 book ai didi

java - Jackson 没有字符串参数构造函数/工厂方法来从字符串值 ('7b70f9fa-3861-4552-a72c-7fca341a4554' 反序列化)

转载 作者:行者123 更新时间:2023-12-02 09:58:08 27 4
gpt4 key购买 nike

Assets

@Column(name = "id")
private UUID id;

// ...

@JsonSerialize(using = TerminalListSerializer.class)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "asset", cascade = {CascadeType.MERGE, CascadeType.REFRESH})
private List<Terminal> terminals;

终端

@Column(name = "id")
private UUID id;

// ...

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="asset_id", foreignKey = @ForeignKey(name = "Terminal_Asset_FK"))
private Asset asset;

@OneToMany(mappedBy = "terminal", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Channel> channels;

channel

@Column(name = "id")
private UUID id;

// ...

@ManyToOne
@JoinColumn(name="term_id", foreignKey = @ForeignKey(name = "Channel_Terminal_FK"))
private Terminal terminal;

自定义序列化程序

public class TerminalListSerializer extends StdSerializer<List<Terminal>> {

public TerminalListSerializer() {
super((Class<List<Terminal>>) null);
}

public TerminalListSerializer(Class<List<Terminal>> t) {
super(t);
}

@Override
public void serialize(List<Terminal> terminals, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException {

List<UUID> ids = new ArrayList<>();
for (Terminal item : terminals) {
ids.add(item.getId());
}
jsonGenerator.writeObject(ids);
}
}

我有 3 个实体,其关系如上所示。为了避免 AssetTerminal 之间的循环引用,我决定使用自定义序列化器。该序列化程序到位后, Assets 将仅包含终端 ID 并解决我的问题。但我现在遇到了另一个问题,因为终端与另一个实体 (Channel) 有关系,当我尝试更新终端时,这会出现此错误。

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `my.package.Terminal` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('7b70f9fa-3861-4552-a72c-7fca341a4554')
at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 1, column: 3320] (through reference chain: my.package.Terminal["channels"]->java.util.HashSet[0]->my.package.Channel["terminal"]->my.package.Terminal["asset"]->my.package.Asset["terminals"]->java.util.ArrayList[0])

最佳答案

您正在将 Terminal 对象列表序列化为 UUID 字符串列表。因此,在反序列化期间会出现错误,因为 Jackson 无法从 String 值创建 Terminal 实例。对终端使用自定义反序列化器或添加 Terminal(String uuid) 构造函数。

关于java - Jackson 没有字符串参数构造函数/工厂方法来从字符串值 ('7b70f9fa-3861-4552-a72c-7fca341a4554' 反序列化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55845319/

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