2017-11-14 3 views
0

私はSpring restTemplateを使用して外部APIを呼び出しています。返されるJSONの内容は次のとおりです。SpringブートプロジェクトでJacksonとJSONをデシリアライズするときに1つのフィールドがnullに割り当てられました

{ 
    "message": null, 
    "responseStatus": "0", 
    "accessInfo": { 
    "access": { 
     "message": "success", 
     "token": { 
     "expires": "xxxxx", 
     "id": null, 
     "UDID": null, 
     "permissions": { 
      "orgGroups": [], 
      "channelGroups": [ 
      "xxxxx" 
      ] 
     } 
     } 
    } 
    }, 
    "profile": { 
    "message": null, 
    "responseStatus": null, 
    "UDID": "xxxxx", 
    "name": null, 
    "age": null, 
    "gender": null, 
    "maritalStatus": null, 
    "familySize": null, 
    "lat": null, 
    "lon": null, 
    "birthDay": null, 
    "imageURL": null, 
    "sessionId": "xxxxx", 
    "FBId": null, 
    "expiry": null, 
    "email": "xxxxx", 
    "workLocationLat": null, 
    "workLocationLong": null, 
    "workLocationAddress": null, 
    "homeLocationAddress": null, 
    "homeLocationLat": null, 
    "homeLocationLong": null, 
    "id": 789, 
    "isFBIdAlreadyExists": false, 
    "tagList": [] 
    } 
} 

すべての値は、私がマスクされたものxxxxxを除き、返却されたものを実際にあります。ご覧のとおり、JSONには2つのUDIDフィールドがあります。 1つはノードaccessInfo>access>tokenであり、これはnullであり、もう1つはノードprofileです。 2番目のノードの値は、興味があるノードで、nullではありません。

以下のように2つのPOJOを作成しました。私はUDIDsessionIdに興味があります。profileノードです。

親POJO:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class AgtAuthentication { 

    private Profile profile; 

} 

子供POJO:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class Profile { 

    private String sessionId; 
    private String UDID; 

} 

私はゲッターとセッターと簡潔にするために、引数を取らないコンストラクタを削除しました。

Apiを呼び出すと、応答が返され、UDID属性を除いてJSONマッピングが正常に機能します。私はレスポンスボディのためSystem.out.println()を行うと、私は以下の結果が得られます。

Authentication [profile = Profile [sessionId = xxxxx, UDID = null]] 

sessionIdは正しいです。ただし、UDIDは常にnullです。 JSONにUDIDの値が重複しているため、これが正しいかどうかはわかりません。誰も助けることができますか?ありがとう。

EDIT:実際のAPIを呼び出すコード(xxxxxがマスクされた値であること)は以下の通りです: - 理由はゲッター/セッターメソッドの命名規則を

private void setUuidAndSessionId() { 
     HttpHeaders headers = new HttpHeaders(); 
     headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); 

     MultiValueMap<String, String> requestBody= new LinkedMultiValueMap<String, String>(); 
     requestBody.add("clientId", "xxxxx"); 
     requestBody.add("clientPassword", "xxxxx"); 
     requestBody.add("UDID", ""); 
     HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(requestBody, headers); 

     RestTemplate restTemplate = new RestTemplate(); 

     String agtAuthUrl = "http://xxxxx"; 

     ResponseEntity<AgtAuthentication> response = restTemplate.exchange(agtAuthUrl, HttpMethod.POST, request, AgtAuthentication.class); 

     System.out.println(response.getBody().toString()); 

     sessionId = response.getBody().getProfile().getSessionId(); 
     String tempUuid = response.getBody().getProfile().getUDID(); 
    } 
+0

Profile.getUDID()ゲッターに注釈を付けることを試みることができますか? –

+0

@XavierBoucletは 'restTemplate'コードスニペットを追加しました。 – swdon

答えて

1

私はジャクソンがJSONのフィールドがuDIDという名前見込んで想定しています最初のフィールドの文字を大文字に変換します。

あなたがrestTemplateにコードを追加することができ@JsonProperty("UDID")

+0

チャームのように働いた!ありがとう。属性自体にプロパティを追加しました。希望は大丈夫です。 – swdon