如何将 Java 中的 Object 响应映射到另一个 ObjectDto

huangapple go评论60阅读模式
英文:

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

{
    &quot;access_token&quot;: &quot;7ada1efc-f159-42fa-84b9-f15b2a0ee333&quot;,
    &quot;refresh_token&quot;: &quot;1c9f5a71-40ae-4979-90db-088c2aa44123&quot;,
    &quot;token_type&quot;: &quot;bearer&quot;,
    &quot;scope&quot;: null,
    &quot;expires_in&quot;: 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&lt;Object&gt; response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                request,
                Object.class,
                &quot;client_credentials&quot;
        );


        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.

如何将 Java 中的 Object 响应映射到另一个 ObjectDto

答案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() == &#39;access_token&#39;) {
            authIntegrationTokenDto.setToken(String.valueOf(entry.getValue()));
            } else if(entry.getKey() == &#39;refresh_token&#39;) {
            authIntegrationTokenDto.setRefreshToken(String.valueOf(entry.getValue()));
            }
        }

huangapple
  • 本文由 发表于 2020年10月17日 13:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64399348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定