提问人:Scripta14 提问时间:3/17/2020 最后编辑:Michał ZioberScripta14 更新时间:10/26/2021 访问量:44287
无法使用 Jackson 从对象值(没有基于委托或基于属性的 Creator)反序列化
Cannot deserialize from Object value (no delegate- or property-based Creator) using Jackson
问:
我正在尝试使用以下有效载荷进行反序列化:JSON
Jackson
{"code":null,"reason":"subscription yet available","message":"{ Message:\"subscription yet available\", SubscriptionUID:\"46b62920-c519-4555-8973-3b28a7a29463\" }"}
但我得到这个:JsonMappingException
Cannot construct instance of `com.ids.utilities.DeserializeSubscription` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"code":null,"reason":"subscription yet available","message":"{ Message:\"subscription yet available\", SubscriptionUID:\"46b62920-c519-4555-8973-3b28a7a29463\" }"}"; line: 1, column: 2]
我创建了两个类。第一堂课:
import lombok.Data;
@Data
public class DeserializeSubscription {
private String code;
private String reason;
private MessageSubscription message;
public DeserializeSubscription(String code, String reason, MessageSubscription message) {
super();
this.code = code;
this.reason = reason;
this.message = message;
}
和二等舱
import lombok.Data;
@Data
public class MessageSubscription {
private String message;
private String subscriptionUID;
public MessageSubscription(String message, String subscriptionUID) {
super();
this.message = message;
this.subscriptionUID = subscriptionUID;
}
在主类中:
try
{
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
DeserializeSubscription desSub=null;
desSub=mapper.readValue(e.getResponseBody(), DeserializeSubscription.class);
System.out.println(desSub.getMessage().getSubscriptionUID());
}
catch (JsonParseException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (JsonMappingException e1) {
System.out.println(e1.getMessage());
e.printStackTrace();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我在应用程序中使用的 jackson maven
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>
答:
17赞
Jens
3/17/2020
#1
信息非常明确:(no Creators, like default construct, exist)
您需要向类或注释添加一个无参数构造函数:NoArgsConstructor
@Data
public class DeserializeSubscription {
public DeserializeSubscription (){}
或
@NoArgsConstructor
@Data
public class DeserializeSubscription {
评论
1赞
Scripta14
3/17/2020
对不起,我之前尝试过第一个解决方案,并使用第二个解决方案获得了相同的结果
0赞
Jens
3/17/2020
@Scripta14* 与第二个解决方案*的结果相同,结果是什么?
0赞
Scripta14
3/17/2020
同样的例外:Cannot construct instance of `com.ids.utilities.DeserializeSubscription` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (String)"{"code":null,"reason":"subscription yet available","message":"{ Message:\"subscription yet available\", SubscriptionUID:\"46b62920-c519-4555-8973-3b28a7a29463\" }"}"; line: 1, column: 2]
6赞
Michał Ziober
3/18/2020
#2
您必须考虑以下几种情况:
message
字段 in 是原始的。在水平上,它是一个对象。JSON
String
POJO
MessageSubscription
message
value in 包含未加引号的属性名称,这是非法的,但也可以处理它们。JSON
Jackson
- 如果构造函数不适合,我们需要使用注释来配置它。
JSON
要处理未加引号的名称,我们需要启用ALLOW_UNQUOTED_FIELD_NAMES功能。为了处理有效负载和之间的不匹配,我们需要为类实现自定义反序列化器。JSON
POJO
MessageSubscription
自定义反串行程序可能如下所示:
class MessageSubscriptionJsonDeserializer extends JsonDeserializer<MessageSubscription> {
@Override
public MessageSubscription deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
final String value = p.getValueAsString();
final Map<String, String> map = deserializeAsMap(value, (ObjectMapper) p.getCodec(), ctxt);
return new MessageSubscription(map.get("Message"), map.get("SubscriptionUID"));
}
private Map<String, String> deserializeAsMap(String value, ObjectMapper mapper, DeserializationContext ctxt) throws IOException {
final MapType mapType = ctxt.getTypeFactory().constructMapType(Map.class, String.class, String.class);
return mapper.readValue(value, mapType);
}
}
现在,我们需要自定义 的构造函数:DeserializeSubscription
@Data
class DeserializeSubscription {
private String code;
private String reason;
private MessageSubscription message;
@JsonCreator
public DeserializeSubscription(
@JsonProperty("code") String code,
@JsonProperty("reason") String reason,
@JsonProperty("message") @JsonDeserialize(using = MessageSubscriptionJsonDeserializer.class) MessageSubscription message) {
super();
this.code = code;
this.reason = reason;
this.message = message;
}
}
如何使用它的例子:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.type.MapType;
import lombok.Data;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class JsonPathApp {
public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();
ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
mapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
DeserializeSubscription value = mapper.readValue(jsonFile, DeserializeSubscription.class);
System.out.println(value);
}
}
对于上面示例打印的提供的有效负载:JSON
DeserializeSubscription(code=null, reason=subscription yet available, message=MessageSubscription(message=subscription yet available, subscriptionUID=46b62920-c519-4555-8973-3b28a7a29463))
评论
0赞
Scripta14
3/18/2020
非常感谢您的解决方案。.不幸的是,我复制了您的代码,但出现此错误:。我已经使用了我的json字符串Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Unexpected end-of-input within/between Object entries at [Source: (String)"{ Message:"; line: 1, column: 3] (through reference chain: test.exatest.DeserializeSubscription["message"])
0赞
Michał Ziober
3/18/2020
@Scripta14,可能你已经破坏了有效载荷。另外,您是否启用了功能?我已经针对您的有效载荷对其进行了测试,并且运行良好。你使用哪个版本?JSON
JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES
Jackson
0赞
Scripta14
3/18/2020
上面我编写了我在应用程序中使用的 Jackson maven 版本。我复制了你的代码。唯一的区别是,我使用了我在这篇文章上面写的 json 字符串。
0赞
Michał Ziober
3/18/2020
@Scripta14,你逃过有效载荷了吗?有效载荷:JSON
"{\"code\":null,\"reason\":\"subscription yet available\",\"message\":\"{ Message:\\\"subscription yet available\\\", SubscriptionUID:\\\"46b62920-c519-4555-8973-3b28a7a29463\\\" }\"}"
1赞
Scripta14
3/18/2020
多谢。。。你的 json 有效负载运行良好,它缺少一些逃生
0赞
david.barkhuizen
10/26/2021
#3
这可能是由于使用了不支持的数据类型,例如无符号整数。
我在反序列化具有 ULong 字段的 JSON 对象时收到此错误。并通过将字段类型更改为正常有符号(长)整数来解决它。
评论