私は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を作成しました。私はUDID
とsessionId
に興味があります。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();
}
と
Profile.getUDID()
ゲッターに注釈を付けることを試みることができますか? –@XavierBoucletは 'restTemplate'コードスニペットを追加しました。 – swdon