亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用于集合類的 Spring Boot 自定義序列化程序

用于集合類的 Spring Boot 自定義序列化程序

至尊寶的傳說 2022-08-17 17:17:44
我試圖為對象的某個屬性實現自定義序列化程序,以便在從 REST 控制器返回對象時獲取不同的 JSON 結構。我的約束是我不能改變REST控制器或模型類的接口(所以我不能添加額外的注釋等,這可能會使這更容易)。我唯一能想到的,使它呈現與模型中描述的不同是自定義序列化程序,如果有更好的方法,請不要猶豫,告訴我一個在約束內的不同方法。我的模型看起來像這樣:public class WrapperModel {  // a lot of autogenerated fields  List<Property> properties;  // getters/setters}public class Property {  private String name;  private String value;  // getters / setters}因此,當它被渲染時,如下所示:{   ....       "properties": [      {"key1": "value1"}, {"key2": "value2"},...       ] }我想要的是這個:{   ....       "properties": {      "key1": "value1",      "key2": "value2",      ...      }}對此的序列化程序非常簡單:public class PropertyListJSONSerializer extends StdSerializer<List<Property>> {//....@Overridepublic void serialize(List<Property> value, JsonGenerator gen,   SerializerProvider provider) throws IOException {    gen.writeStartObject();    for(Property p: value){        gen.writeStringField(p.getName(), p.getValue());    }    gen.writeEndObject();}}現在,當我嘗試在文件中注冊此序列化程序時:@Configuration@Beanpublic ObjectMapper objectMapper() {    ObjectMapper mapper = new ObjectMapper();    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);    SimpleModule module = new SimpleModule();    module.addSerializer(List<Property>.class, new PropertyListJSONSerializer());    mapper.registerModule(module);    return mapper;}這不起作用,因為它是一個模板類,因此不能用于。有沒有其他方法可以添加此序列化程序或執行類似操作的內容?List<Property>.classaddSerializer我不想添加自定義序列化程序,因為此類是自動生成的,并且可以添加和刪除字段。這應該可以在不修改應用程序代碼的情況下實現(如果我有一個自定義序列化程序,您還需要從序列化程序中添加/刪除字段(?))。或者,有沒有辦法只對類使用標準序列化程序,然后手動處理這一個字段。WrapperModelList<>模型類由Spring Boot openapi代碼生成器生成,因此我可以將一組非常有限的JSON注釋放在模型字段之上(如果有注釋方式,請不要猶豫,因為如果支持該特定注釋,我可以簽入openapi源代碼)。但是,如果可能的話,我寧愿使用自定義序列化程序,或者編寫一個用于所有內容并且僅自己處理屬性的序列化程序。List<Property>WrapperModelStdSerializerList
查看完整描述

1 回答

?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

米辛

在這種情況下,我們需要使用功能。創建如下界面:MixIn


interface WrapperModelMixIn {


    @JsonSerialize(using = PropertyListJSONSerializer.class)

    List<Property> getProperties();

}

并按如下方式注冊:


ObjectMapper mapper = new ObjectMapper();

mapper.addMixInAnnotations(WrapperModel.class, WrapperModelMixIn.class);

較舊的提案

您需要使用允許為泛型類型注冊序列化器的類型。更改后的序列化程序可能如下所示:Jackson


class PropertyListJSONSerializer extends StdSerializer<List<Property>> {


    public PropertyListJSONSerializer(JavaType type) {

        super(type);

    }


    @Override

    public void serialize(List<Property> value, JsonGenerator gen, SerializerProvider provider)

        throws IOException {

        gen.writeStartObject();

        for (Property p : value) {

            gen.writeStringField(p.getName(), p.getValue());

        }

        gen.writeEndObject();

    }

}

您可以按如下方式注冊:


ObjectMapper mapper = new ObjectMapper();


CollectionType propertiesListType = mapper.getTypeFactory().constructCollectionType(List.class, Property.class);

SimpleModule module = new SimpleModule();

module.addSerializer(new PropertyListJSONSerializer(propertiesListType));

mapper.registerModule(module);


查看完整回答
反對 回復 2022-08-17
  • 1 回答
  • 0 關注
  • 112 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號