英文:
How to Map Object response to another ObjectDto in java
问题
I have to map rest template response to my DTO with different key and values.
currently I am getting this json response from rest api
{
"access_token": "7ada1efc-f159-42fa-84b9-f15b2a0ee333",
"refresh_token": "1c9f5a71-40ae-4979-90db-088c2aa44123",
"token_type": "bearer",
"scope": null,
"expires_in": 1440
}
And I want to map it into my DTO for me able to save into DB
@Data
public class AuthIntegrationTokenDto {
private long id;
private int cmsIntegrationId;
private String token;
private String refreshToken;
private String createdBy;
private String lastUpdatedBy;
}
What i want is to get only same key dynamically to match with the response of api above.
Currently I am doing this but it seems that I am not setting correct value of same keys.
ResponseEntity<Object> response = restTemplate.exchange(
url,
HttpMethod.POST,
request,
Object.class,
"client_credentials"
);
Object result = response.getBody();
JSONObject json = new JSONObject((Map) result);
AuthIntegrationTokenDto authIntegrationTokenDto = new AuthIntegrationTokenDto();
for (Object o : json.entrySet()) {
Map.Entry entry = (Map.Entry) o;
authIntegrationTokenDto.setToken(String.valueOf(entry.getValue()));
authIntegrationTokenDto.setRefreshToken(String.valueOf(entry.getValue()));
}
After executing this I am getting null values in my db.
英文:
I have to map rest template response to my DTO with different key and values.
currently I am getting this json response from rest api
{
"access_token": "7ada1efc-f159-42fa-84b9-f15b2a0ee333",
"refresh_token": "1c9f5a71-40ae-4979-90db-088c2aa44123",
"token_type": "bearer",
"scope": null,
"expires_in": 1440
}
And I want to map it into my DTO for me able to save into DB
@Data
public class AuthIntegrationTokenDto {
private long id;
private int cmsIntegrationId;
private String token;
private String refreshToken;
private String createdBy;
private String lastUpdatedBy;
}
What i want is to get only same key dynamically to match with the response of api above.
Currently I am doing this but it seems that I am not setting correct value of same keys.
ResponseEntity<Object> response = restTemplate.exchange(
url,
HttpMethod.POST,
request,
Object.class,
"client_credentials"
);
Object result = response.getBody();
JSONObject json = new JSONObject((Map) result);
AuthIntegrationTokenDto authIntegrationTokenDto = new AuthIntegrationTokenDto();
for (Object o : json.entrySet()) {
Map.Entry entry = (Map.Entry) o;
authIntegrationTokenDto.setToken(String.valueOf(entry.getValue()));
authIntegrationTokenDto.setRefreshToken(String.valueOf(entry.getValue()));
}
After executing this I am getting null values in my db.
答案1
得分: 1
你没有正确地将值设置到DTO中。您必须首先获取键,然后再进行设置:
for (Object o : json.entrySet()) {
Map.Entry entry = (Map.Entry) o;
if(entry.getKey().equals("access_token")) {
authIntegrationTokenDto.setToken(String.valueOf(entry.getValue()));
} else if(entry.getKey().equals("refresh_token")) {
authIntegrationTokenDto.setRefreshToken(String.valueOf(entry.getValue()));
}
}
英文:
You are not setting the values to the DTO correctly. You must get the key first and then set it:
for (Object o : json.entrySet()) {
Map.Entry entry = (Map.Entry) o;
if(entry.getKey() == 'access_token') {
authIntegrationTokenDto.setToken(String.valueOf(entry.getValue()));
} else if(entry.getKey() == 'refresh_token') {
authIntegrationTokenDto.setRefreshToken(String.valueOf(entry.getValue()));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论