第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

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

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

至尊寶的傳說 2022-08-17 17:17:44
我試圖為對(duì)象的某個(gè)屬性實(shí)現(xiàn)自定義序列化程序,以便在從 REST 控制器返回對(duì)象時(shí)獲取不同的 JSON 結(jié)構(gòu)。我的約束是我不能改變REST控制器或模型類的接口(所以我不能添加額外的注釋等,這可能會(huì)使這更容易)。我唯一能想到的,使它呈現(xiàn)與模型中描述的不同是自定義序列化程序,如果有更好的方法,請(qǐng)不要猶豫,告訴我一個(gè)在約束內(nèi)的不同方法。我的模型看起來像這樣:public class WrapperModel {  // a lot of autogenerated fields  List<Property> properties;  // getters/setters}public class Property {  private String name;  private String value;  // getters / setters}因此,當(dāng)它被渲染時(shí),如下所示:{   ....       "properties": [      {"key1": "value1"}, {"key2": "value2"},...       ] }我想要的是這個(gè):{   ....       "properties": {      "key1": "value1",      "key2": "value2",      ...      }}對(duì)此的序列化程序非常簡(jiǎn)單: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();}}現(xiàn)在,當(dāng)我嘗試在文件中注冊(cè)此序列化程序時(shí):@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;}這不起作用,因?yàn)樗且粋€(gè)模板類,因此不能用于。有沒有其他方法可以添加此序列化程序或執(zhí)行類似操作的內(nèi)容?List<Property>.classaddSerializer我不想添加自定義序列化程序,因?yàn)榇祟愂亲詣?dòng)生成的,并且可以添加和刪除字段。這應(yīng)該可以在不修改應(yīng)用程序代碼的情況下實(shí)現(xiàn)(如果我有一個(gè)自定義序列化程序,您還需要從序列化程序中添加/刪除字段(?))?;蛘?,有沒有辦法只對(duì)類使用標(biāo)準(zhǔn)序列化程序,然后手動(dòng)處理這一個(gè)字段。WrapperModelList<>模型類由Spring Boot openapi代碼生成器生成,因此我可以將一組非常有限的JSON注釋放在模型字段之上(如果有注釋方式,請(qǐng)不要猶豫,因?yàn)槿绻С衷撎囟ㄗ⑨?,我可以簽入openapi源代碼)。但是,如果可能的話,我寧愿使用自定義序列化程序,或者編寫一個(gè)用于所有內(nèi)容并且僅自己處理屬性的序列化程序。List<Property>WrapperModelStdSerializerList
查看完整描述

1 回答

?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

米辛

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


interface WrapperModelMixIn {


    @JsonSerialize(using = PropertyListJSONSerializer.class)

    List<Property> getProperties();

}

并按如下方式注冊(cè):


ObjectMapper mapper = new ObjectMapper();

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

較舊的提案

您需要使用允許為泛型類型注冊(cè)序列化器的類型。更改后的序列化程序可能如下所示: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();

    }

}

您可以按如下方式注冊(cè):


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);


查看完整回答
反對(duì) 回復(fù) 2022-08-17
  • 1 回答
  • 0 關(guān)注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)