-1

我正在尝试将 json 字符串反序列化为 Card.class,但我无法做到。

    Card card =new Card();
    card.setHeader(new CardHeader().setTitle("HelloWorld"));
    Section section=new Section().setWidgets(Collections.singletonList(new WidgetMarkup().setImage(new Image().setImageUrl("https://gazabb//..com"))));
    card.setSections(Collections.singletonList(section));
    String req=GsonUtils.GSON.toJson(card,Card.class);
    System.out.println(req);

输出符合预期

{"header":{"title":"HelloWorld"},"sections":[{"widgets":[{"image":{"imageUrl":"https://gazabb//..com"}}]}]}

但是当我试图将它序列化为 Card.class 即

GsonUtils.GSON.fromJson(req,Card.class);

它给了我错误:

java.lang.IllegalArgumentException: Can not set com.google.api.services.chat.v1.model.CardHeader field com.google.api.services.chat.v1.model.Card.header to com.google.gson.internal.LinkedTreeMap
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:275)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:231)
at com.google.api.client.util.GenericData.put(GenericData.java:98)
at com.google.api.client.util.GenericData.put(GenericData.java:43)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:188)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at io.gsonfire.gson.HooksTypeAdapter.deserialize(HooksTypeAdapter.java:86)
at io.gsonfire.gson.HooksTypeAdapter.read(HooksTypeAdapter.java:54)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.google.gson.Gson.fromJson(Gson.java:813)

Card.class 是:

package com.google.api.services.chat.v1.model;
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Data;
import com.google.api.client.util.Key;
import java.util.List;

public final class Card extends GenericJson {
@Key
private List<CardAction> cardActions;
@Key
private CardHeader header;
@Key
private String name;
@Key
private List<Section> sections;

public Card() {
}

public List<CardAction> getCardActions() {
    return this.cardActions;
}

public Card setCardActions(List<CardAction> var1) {
    this.cardActions = var1;
    return this;
}

public CardHeader getHeader() {
    return this.header;
}

public Card setHeader(CardHeader var1) {
    this.header = var1;
    return this;
}

public String getName() {
    return this.name;
}

public Card setName(String var1) {
    this.name = var1;
    return this;
}

public List<Section> getSections() {
    return this.sections;
}

public Card setSections(List<Section> var1) {
    this.sections = var1;
    return this;
}

public Card set(String var1, Object var2) {
    return (Card)super.set(var1, var2);
}

public Card clone() {
    return (Card)super.clone();
}

static {
    Data.nullOf(CardAction.class);
    Data.nullOf(Section.class);
}

CardHeader 类:

package com.google.api.services.chat.v1.model;

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;

public final class CardHeader extends GenericJson {
@Key
private String imageStyle;
@Key
private String imageUrl;
@Key
private String subtitle;
@Key
private String title;

public CardHeader() {
}

public String getImageStyle() {
    return this.imageStyle;
}

public CardHeader setImageStyle(String var1) {
    this.imageStyle = var1;
    return this;
}

public String getImageUrl() {
    return this.imageUrl;
}

public CardHeader setImageUrl(String var1) {
    this.imageUrl = var1;
    return this;
}

public String getSubtitle() {
    return this.subtitle;
}

public CardHeader setSubtitle(String var1) {
    this.subtitle = var1;
    return this;
}

public String getTitle() {
    return this.title;
}

public CardHeader setTitle(String var1) {
    this.title = var1;
    return this;
}

public CardHeader set(String var1, Object var2) {
    return (CardHeader)super.set(var1, var2);
}

public CardHeader clone() {
    return (CardHeader)super.clone();
}

}

我坚持了两天。从错误中可以清楚地看到 Card 的 CardHeader 字段无法设置为 LinkedTreeMap。但我无法找到它的原因。是否可以在不编写我自己的 TypeAdaptor 的情况下对其进行反序列化。我也尝试对杰克逊做同样的事情,但面临同样的错误。

4

1 回答 1

0

超类型中是否有可能与您的字段冲突的标题字段。删除"header":{"title":"HelloWorld"},错误消失。

于 2021-11-09T05:22:28.417 回答