You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Such as the code, I need to call it,the interface has different performance depends on the Object passed.
However AbsMessage is a abstract class,when I call it need to pass an object which instance of AbsMessage.
But the reuest body doesn't conatin any field from the runtime sub class whic I passed. All fields are from the abstract class.
I find the cause at last.
@DatastaticabstractclassMessage {
StringtoUser;
publicMessage(StringtoUser) {
this.toUser = toUser;
}
}
@Getter@Setter@EqualsAndHashCode(callSuper = true)
publicstaticclassSimpleTextMessageextendsMessage {
Stringcontent;
Integertype;
publicSimpleTextMessage(StringtoUser, Stringcontent, Integertype) {
super(toUser);
this.content = content;
this.type = type;
}
}
@TestvoidgsonTest() {
Gsongson = newGsonBuilder().create();
SimpleTextMessagemessage = newSimpleTextMessage("测试用户", "测试消息", Integer.valueOf(1));
varres = gson.toJson(message, Message.class);
// the final result only include toUser prop,bus loss content type props.// result: {"toUser":"测试用户"}System.out.println(res);
}
Here are two classes,just like the lib feign-gson,it serializes object with the type declared,but not runtime real type.
This leads to a situation the json string not contains the props from the runtime real subclasses.
Thanks to your reply.
Such as the code, I need to call it,the interface has different performance depends on the Object passed.
However AbsMessage is a abstract class,when I call it need to pass an object which instance of AbsMessage.
But the reuest body doesn't conatin any field from the runtime sub class whic I passed. All fields are from the abstract class.
I find the cause at last.
It serializes object with the type which declared in the function,not runtime type of the passed object.
If gson.toJson(object),It appears normal.
The text was updated successfully, but these errors were encountered: