2017-08-16 156 views
0

JSON動態字段名我使用從它我生成下面的類提供了一個swagger.yaml API:與傑克遜

@ApiModel(description="the paginated history of the specification attributes values") 
public class SpecificationHistoryResponse { 

    @ApiModelProperty(example = "null", value = "the array of historic values is named with the specification attributes key") 
    private List<SpecificationResponse> key = new ArrayList<SpecificationResponse>(); 
    @ApiModelProperty(example = "null", value = "") 
    private Pagination pagination = null; 

/** 
    * the array of historic values is named with the specification attributes key 
    * @return key 
    **/ 
    public List<SpecificationResponse> getKey() { 
    return key; 
    } 

    public void setKey(List<SpecificationResponse> key) { 
    this.key = key; 
    } 

    public SpecificationHistoryResponse key(List<SpecificationResponse> key) { 
    this.key = key; 
    return this; 
    } 

    public SpecificationHistoryResponse addKeyItem(SpecificationResponse keyItem) { 
    this.key.add(keyItem); 
    return this; 
    } 

/* ... */ 
} 

使用API​​以請求以下JSON一個特定的「規範」返回一個SpecificationHistoryRespone

{ 
    "specification_key": [ 
    { 
     "value": "0.02242", 
     "source_timestamp": "2017-08-09T13:10:04.177Z" 
    }, 
    { 
     "value": "0.0124", 
     "source_timestamp": "2017-08-11T13:16:04.177Z" 
    } 
    /*...*/ 
    ], 
    "pagination": { 
    /*...*/ 
    } 
} 

使用JacksonJsonProvider我不能得到specification_key,因爲它總是試圖反序列化值key不存在。

回答

0

好自動生成的代碼需要相當長的一段編輯應該是這樣的動態字段名工作:

@ApiModel(description="the paginated history of the specification attributes values") 
public class SpecificationHistoryResponse { 

    @ApiModelProperty(example = "null", value = "the array of historic values is named with the specification attributes key") 
    @JsonAnySetter 
    private Map<String, List<SpecificationResponse>> key = new HashMap<>(); 
    @ApiModelProperty(example = "null", value = "") 
    private Pagination pagination = null; 

/** 
    * the array of historic values is named with the specification attributes key 
    * @return key 
    **/ 
    public Map<String, List<SpecificationResponse>> getKey() { 
    return key; 
    } 

    public void setKey(Map<String, List<SpecificationResponse>> key) { 
    this.key = key; 
    } 

    public SpecificationHistoryResponse key(Map<String, List<SpecificationResponse>> key) { 
    this.key = key; 
    return this; 
    } 
/* ... */ 
}