如何使用Jackson库序列化反序列化指定对象?解决TPType_T反序列化报错
Jackson序列化/反序列化问题解决:Notification2类报错处理
你要对以下Java对象使用Jackson进行序列化与反序列化:
public class Notification2 implements Serializable { private String eventTypeName; private TerminationPoint_T tpObj; public Notification2() { } public TerminationPoint_T getTpObj() { return tpObj; } public void setTpObj(TerminationPoint_T tpObj) { this.tpObj = tpObj; } } public final class TerminationPoint_T implements org.omg.CORBA.portable.IDLEntity { public org.tmforum.mtnm.terminationPoint.TPType_T type; public TerminationPoint_T( org.tmforum.mtnm.terminationPoint.TPType_T type) { this.type = type; } }
执行过程中出现报错:
com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法构造
org.tmforum.mtnm.terminationPoint.TPType_T的实例(尽管存在至少一个构造器):无法从对象值反序列化(无委托或基于属性的构造器)
at [Source: (String)
报错原因
核心问题是Jackson无法找到合适的构造器实例化TPType_T类,同时TerminationPoint_T仅提供带参构造,也会给反序列化造成障碍。Jackson默认要求目标类具备无参构造器,或者通过注解明确指定可用于反序列化的带参构造逻辑。
解决方案
方案1:给涉及类添加无参构造器
如果可以修改TPType_T和TerminationPoint_T的源码,添加无参构造是最直接的解决方式:
- 给
TPType_T添加无参构造:
public class TPType_T { // 原有字段与方法 public TPType_T() {} // 新增无参构造 }
- 给
TerminationPoint_T添加无参构造:
public final class TerminationPoint_T implements org.omg.CORBA.portable.IDLEntity { public org.tmforum.mtnm.terminationPoint.TPType_T type; public TerminationPoint_T() {} // 新增无参构造 public TerminationPoint_T(org.tmforum.mtnm.terminationPoint.TPType_T type) { this.type = type; } }
方案2:用Jackson注解指定构造器(无法修改源码时)
如果无法修改目标类源码,使用Jackson的@JsonCreator和@JsonProperty注解标记构造器,明确JSON字段与构造参数的映射关系:
- 对
TPType_T的构造器标注:
public class TPType_T { private String typeValue; @JsonCreator // 标记该构造器用于反序列化 public TPType_T(@JsonProperty("typeValue") String typeValue) { // 指定对应JSON字段名 this.typeValue = typeValue; } }
- 对
TerminationPoint_T的构造器标注:
public final class TerminationPoint_T implements org.omg.CORBA.portable.IDLEntity { public org.tmforum.mtnm.terminationPoint.TPType_T type; @JsonCreator public TerminationPoint_T(@JsonProperty("type") org.tmforum.mtnm.terminationPoint.TPType_T type) { this.type = type; } }
方案3:自定义序列化/反序列化器(CORBA生成类场景)
如果TPType_T是CORBA IDL生成的特殊类,可自定义序列化/反序列化逻辑适配:
import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.core.JsonParser; import java.io.IOException; // 自定义TPType_T反序列化器 public class TPType_TDeserializer extends StdDeserializer<TPType_T> { public TPType_TDeserializer() { super(TPType_T.class); } @Override public TPType_T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { String value = p.getText(); // 根据CORBA类实际构造逻辑创建实例,这里假设存在静态工厂方法 return TPType_T.fromString(value); } }
注册该反序列化器到ObjectMapper:
ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new com.fasterxml.jackson.databind.module.SimpleModule() .addDeserializer(TPType_T.class, new TPType_TDeserializer()));
测试代码示例
import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTest { public static void main(String[] args) throws Exception { ObjectMapper mapper = new ObjectMapper(); // 序列化操作 Notification2 notify = new Notification2(); notify.setEventTypeName("PortStatusChange"); TerminationPoint_T tp = new TerminationPoint_T(new TPType_T("USER_PORT")); notify.setTpObj(tp); String jsonStr = mapper.writeValueAsString(notify); System.out.println("序列化JSON:" + jsonStr); // 反序列化操作 Notification2 result = mapper.readValue(jsonStr, Notification2.class); System.out.println("反序列化事件名:" + result.getEventTypeName()); System.out.println("反序列化TP类型:" + result.getTpObj().type); } }
内容的提问来源于stack exchange,提问作者Techiee
相关产品推荐
相关产品推荐

