2017-07-21 6 views
0

をPOJOに取り組んでいない私はJSONを取得しています:春を使用してAndroidアプリでのWeb APIから春Androidのマッピングは

[{"LocationId":1,"LocationCode":"Area A"},{"LocationId":2,"LocationCode":"Area B"}] 

が、そのは、POJOにマップするために失敗:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class DataLocation { 
    private String LocationId; 
    private String LocationCode; 

    public String getLocationId(){ 
     return LocationId; 
    } 
    public String getLocationCode(){ 
     return LocationCode; 
    } 
} 

これは、 jsonを取得するコード:

RestTemplate restTemplate = new RestTemplate(); 
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); 
ResponseEntity<DataLocation[]> forNow = restTemplate.getForEntity(url, DataLocation[].class); 

enter image description here

地図作成に失敗した理由は誰でも知っていますか?

EDIT

このlinkspring経由jsonに変換することができpojoを作成するために助けました。だから、結果POJOは、次のようになります。

@JsonInclude(JsonInclude.Include.NON_NULL) 
@JsonPropertyOrder({ 
     "LocationId", 
     "LocationCode" 
}) 
public class DataLocation { 

    @JsonProperty("LocationId") 
    private Integer locationId; 
    @JsonProperty("LocationCode") 
    private String locationCode; 
    @JsonIgnore 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    @JsonProperty("LocationId") 
    public Integer getLocationId() { 
     return locationId; 
    } 

    @JsonProperty("LocationId") 
    public void setLocationId(Integer locationId) { 
     this.locationId = locationId; 
    } 

    @JsonProperty("LocationCode") 
    public String getLocationCode() { 
     return locationCode; 
    } 

    @JsonProperty("LocationCode") 
    public void setLocationCode(String locationCode) { 
     this.locationCode = locationCode; 
    } 

    @JsonAnyGetter 
    public Map<String, Object> getAdditionalProperties() { 
     return this.additionalProperties; 
    } 

    @JsonAnySetter 
    public void setAdditionalProperty(String name, Object value) { 
     this.additionalProperties.put(name, value); 
    } 

} 
+0

など、あなたがセッターに奇数値を変換する必要がある場合は特に、非常に便利なツールであることはできません?奇数の場合については

私は1つを生成することができました[ここ](http://jsonschema2pojo.org) –

+0

GSONはjsonをPOJOに解析する方が良いです。 – Kintanpatel

+0

私は 'List 'を得て、それを 'List ' – JustLearning

答えて

1

あなたが興味があれば、そのようなあなたのフィールドの名前を大文字にするには非常に悪い習慣ですので、理由が理由と考えられます。

通常、JSONが標準のcamelCase、kebab-case、snake_caseにある場合、Springはこれらのマッピングを処理できます。それは@JsonPropertyはあなたのPOJOを生成しようとしましたか

関連する問題