2017-08-16 1 views
0

Ich verwende eine API, die eine swagger.yaml davon liefert ich folgende Klasse erzeugt:JSON dynamische Feldnamen mit Jackson

@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; 
    } 

/* ... */ 
} 

Verwendung der API einen SpecificationHistoryRespone für eine bestimmte „Spezifikation“ liefert folgende JSON zu beantragen:

{ 
    "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": { 
    /*...*/ 
    } 
} 

die JacksonJsonProvider Verwendung kann ich nicht specification_key bekommen, wie es immer einen Wert key deserialisieren versucht, die es nicht gibt.

Antwort

0

Okay, die automatisch erstellt Code ganz einige Bearbeitung benötigt und soll wie folgt aussieht mit dynamischen Feldnamen zu arbeiten:

@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; 
    } 
/* ... */ 
} 
Verwandte Themen