我使用jackson 2将json转换为java对象。到现在为止还挺好。但我也使用hazelcast来分发集群中的对象。因此,所有bean都必须是java.io.Serializable。当我从json中读取Object时:
ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(AbstractBean.class, MongoIdMixIn.class);
// this is to prevent from failing on missing type class property: @JsonProperty("@class")
Object tgtObject = targetClass.newInstance();
mapper.readerForUpdating(tgtObject).readValue(dbo.toString());
// put into hazelcast map
target.put(dbo.get(keyColumn), tgtObject);
我将从hazelcast获得例外:
java.io.NotSerializableException: com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer
我想知道com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer来自哪里,因为Object是普通的java bean(但是使用继承)。
我的抽象类是:
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@javaClass")
public abstract class AbstractBean implements Serializable {
@JsonIgnore public static final transient IMarkupParser MARKUP_PARSER = new WikiMarkupParser();
@JsonProperty("id")
private String id;
@JsonProperty("@class")
private String clazz;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return this.getClass().getSimpleName();
}
}
我的孩子是:
public class Posting extends AbstractBean {
private String postingSource;
private String languageCode;
public String getPostingSource() {
return postingSource;
}
public void setPostingSource(String postingSource) {
this.postingSource = postingSource;
}
public String getLanguageCode() {
return languageCode;
}
public void setLanguageCode(String languageCode) {
this.languageCode = languageCode;
}
}
我不知道为什么serailizer甚至会尝试序列化mixin,因为它们不是bean的一部分但是在这里它们是(是的,我试图让它们也可序列化,就像测试一样,没有运气):< / p>
public interface IdMixins extends Serializable {
}
public interface MongoIdMixIn extends IdMixins {
@JsonProperty("_id")
@JsonSerialize(using = MongoIdSerializer.class)
public String getId();
@JsonProperty("_id")
@JsonDeserialize(using = MongoIdDeserializer.class)
public void setId(String id);
}
public class MongoIdDeserializer extends JsonDeserializer<String> implements Serializable {
private static final long serialVersionUID = -5404276857799190647L;
@Override
public String deserialize(JsonParser jp, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String value = null;
String tmp = jp.getText(); // {
validate(jp, tmp,"{");
int curly = 1;
while (jp.nextToken() != null) {
String v = jp.getText();
if (v.equals("{")) curly++;
if (v.equals("$oid")) {
jp.nextToken();
value = jp.getText();
}
if (v.equals("}")) curly--;
if (curly<=0) return value;
}
return null;
}
private void validate(JsonParser jsonParser, String input, String expected) throws JsonProcessingException {
if (!input.equals(expected)) {
throw new JsonParseException("Unexpected token: " + input, jsonParser.getTokenLocation());
}
}
}
public class MongoIdSerializer extends JsonSerializer<String> implements Serializable {
private static final long serialVersionUID = 3435689991839324194L;
@Override
public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("$oid");
jsonGenerator.writeString(s);
jsonGenerator.writeEndObject();
}
}
答案 0 :(得分:1)
愚蠢的我!序列化链中的某个地方是一个完全不必要的ObjectMapper对象。但很难找到,因为Posting对象不是真正的原因,而是另一个对象。但Stacktrace和com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer异常完全错过领先! ...集群软件有时候很难调试: - )
答案 1 :(得分:0)
我是1 Rep。指出能否发表评论。所以我必须提出一个建议作为答案;-)。
也许其中一个Annotations将TypeWrappedDeserializer的实例作为私有属性注入AbstractBean。也许是对反序列化机制的暗示。
您可以使用反射检查创建的对象以进行验证吗?
for (Field field : tgtObject.getClass().getDeclaredFields() )
{
// you can replace this by your logging method
System.out.println("Field: " + field.getName() + ":" + field.getType());
}
for (Field field : tgtObject.getClass().getSuperclass().getDeclaredFields() )
{
// you can replace this by your logging method
System.out.println("Field: " + field.getName() + ":" + field.getType());
}
如果您在列表中找到适当的类型,则通过字节代码增强添加类。