스프링 restTemplate
을 사용하여 외부 API를 호출합니다. 반환 된 JSON에는 다음과 같은 내용이 있습니다.Spring 부트 프로젝트에서 Jackson과 JSON을 deserialize 할 때 하나의 필드가 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에는 두 개의 UDID
필드가 있습니다. 하나는 노드 accessInfo
>access
>token
이고 null
인 노드와 다른 노드는 profile
입니다. 두 번째 노드의 값은 내가 관심이있는 노드이며 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;
}
나는 getter 및 setter 및 간결 없음 인수 생성자를 제거했습니다.
API를 호출하면 UDID
속성을 제외하고 JSON 매핑이 제대로 작동합니다. 내가 응답 본문에 대한 System.out.println()
를 할 때, 나는 아래의 결과를 얻을 :
Authentication [profile = Profile [sessionId = xxxxx, UDID = null]]
sessionId
가 올바른지. 그러나 UDID
은 항상 null
입니다. JSON에 UDID
값이 중복되어 있기 때문에 이것이 맞는지 확실하지 않습니다. 누구든지 도와 줄 수 있습니까? 감사.
편집 : (xxxxx
값을 마스크되는) 실제 API를 호출하는 코드는 다음과 같습니다 :
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