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

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

從 json 到包含枚舉的對象的反序列化問題

從 json 到包含枚舉的對象的反序列化問題

偶然的你 2023-04-26 17:12:13
使用 jackson2.9.8 進行枚舉反序列化時出現問題。同樣適用于 Gson。模型是從 swagger api 定義自動生成的使用 Gson,它工作正常。對于 jackson,它不適用于 011、013 和 019,但適用于其他值Swagger API 定義的片段服務代碼:類型:字符串枚舉:-“001”-“002”-“003”-“004”-“005”-“007”-“008”-“009”-“011”-“013”-“019” “自動生成的代碼(為了可讀性刪除了 getter/setter)public class ProcessErrorTest1 {    static String errorJson =            "    {\n" +            "      \"timestamp\": \"2019-07-29 11:55:48\",\n" +            "      \"serviceCode\": \"019\",\n" +            "      \"message\": \"service failed. \",\n" +            "      \"rootException\": {\n" +            "        \"source\": \"test\",\n" +            "        \"reasonCode\": \"2131\",\n" +            "        \"reasonText\": \"test\"\n" +            "      }\n" +            "}";    public static void main(String[] args) throws Exception {        ObjectMapper mapper = new ObjectMapper();        AppErrorResponse error = mapper.readValue(errorJson, AppErrorResponse.class);       // Gson gson = new Gson();        //AppErrorResponse error = gson.fromJson(errorJson, AppErrorResponse.class);        System.out.println("error::" + error);    }}public class AppErrorResponse {  @SerializedName("message")  private String message = null;  @SerializedName("rootException")  private ErrorResponse rootException = null;  /**   * Gets or Sets serviceCode   */  @JsonAdapter(ServiceCodeEnum.Adapter.class)  public enum ServiceCodeEnum {    _001("001"),    _002("002"),    _003("003"),     _004("004"),    _005("005"),    _007("007"),    _008("008"),    _009("009"),    _011("011"),    _013("013"),   // @JsonProperty("019") if add this , it works fine. But can't modify the auto generated code as it is available as jar    _019("019");  }  @SerializedName("serviceCode")  private ServiceCodeEnum serviceCode = null;  @SerializedName("timestamp")  private String timestamp = null;}
查看完整描述

1 回答

?
侃侃爾雅

TA貢獻1801條經驗 獲得超16個贊

您的注釋針對 Gson 而不是 Jackson,您應該為 Jackson 而不是 Gson 生成 pojo。該行@JsonAdapter(ServiceCodeEnum.Adapter.class)是一個適配器,它處理 Gson 的枚舉轉換。

那里說:For all the unspecified options default values will be used.

使用上面列出的下表:

CONFIG OPTIONS

? ? modelPackage

? ? ? ? package for generated models


? ? apiPackage

? ? ? ? package for generated api classes

...... (results omitted)

? ? library

? ? ? ? library template (sub-template) to use:

? ? ? ? jersey1 - HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2

? ? ? ? jersey2 - HTTP client: Jersey client 2.6

? ? ? ? feign - HTTP client: Netflix Feign 8.1.1.? JSON processing: Jackson 2.6.3

? ? ? ? okhttp-gson (default) - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1

? ? ? ? retrofit - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)

? ? ? ? retrofit2 - HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta2)

? ? ? ? google-api-client - HTTP client: google-api-client 1.23.0. JSON processing: Jackson 2.8.9

? ? ? ? rest-assured - HTTP client: rest-assured : 3.1.0. JSON processing: Gson 2.6.1. Only for Java8

這條線可能是什么被使用:


okhttp-gson (default) - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1


您需要指定一個使用 Jackson 的庫和您想要的 HTTP 客戶端。


查看完整回答
反對 回復 2023-04-26
  • 1 回答
  • 0 關注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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