gpt4 book ai didi

java - jackson 枚举属性(property)没有得到认可

转载 作者:搜寻专家 更新时间:2023-11-01 02:23:03 26 4
gpt4 key购买 nike

我有一个像这样的枚举:

public enum Type {
@JsonProperty("private")Private, group, supergroup, channel
}

json 文件本身有私有(private)字段,所以我需要私有(private)(这是 java 键盘)来进行序列化和映射,但是如果我使用 private 并且所有小写都是运行时错误。我在上面的代码中使用了 @JsonProperty,但仍然无法正常工作。

你有什么解决办法吗?

这里是个异常(exception):

com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of com.mehdi.model.Chat$Type from String value 'private': value not one of declared Enum instance names: [Private, group, supergroup, channel] at [Source: /home/mehdi/Desktop/json.txt; line: 14, column: 16] (through reference chain: com.mehdi.model.Update["message"]->com.mehdi.model.Message["chat"]->com.mehdi.model.Chat["type"]) at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55) at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:742) at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:91) at com.fasterxml.jackson.databind.deser.std.EnumDeserializer.deserialize(EnumDeserializer.java:20) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:525) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:99) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:242) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:118) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2052) at com.mehdi.ff.Test.main(Test.java:25) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Process finished with exit code 0

最佳答案

您必须在枚举实现中使用 @JsonValue

public enum Type {

Private("private"),
Group("group"),
Supergroup("supergroup"),
Channel("channel")

private String jsonValue;

private Type(final String json) {
this.jsonValue = json;
}

@JsonValue
public String jsonValue() {
return this.jsonValue;
}

}

关于java - jackson 枚举属性(property)没有得到认可,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33890183/

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