简体   繁体   中英

How to parse a single json object responses?

I am trying to parse/convert a get response to a POJO. I have the User class below and the Controller class.

In the getUsers() method, I am able to parse the request by using ParameterizedTypeReference with wrapper class. However, I am not able to parse the "single object" response.

For getUser response, if I use the same code by changing the ParameterizedTypeReference<UsersWrapper>() as ParameterizedTypeReference<User>() or use the code shown in the getUser method, I get null values for my User class.

I assume I am not able to parse the JSON because of the { "user": {.. wrapper.

How can I have the values for the User class from JSON by using:

@GetMapping("/{id}")
public User getUser(@PathVariable long id) {}

JSON:

    {
    "user": {
        "id": 375607441100,
        "url": "https://example.com/users/375607441100.json",
        "name": "example-user",
        "email": "example-user@hotmail.com",
        "created_at": "2020-11-25T09:13:40Z",
        "updated_at": "2020-11-25T09:16:44Z",
        "time_zone": "Europe/Warsaw",
        "iana_time_zone": "Europe/Warsaw",
        "phone": null,
        "shared_phone_number": null,
        "photo": null,
        "locale_id": 1,
        "locale": "en-US",
        "organization_id": null,
        "role": "admin",
        "verified": true,
        "external_id": null,
        "tags": [],
        "alias": null,
        "active": true,
        "shared": false,
        "shared_agent": false,
        "last_login_at": "2020-11-25T09:16:35Z",
        "two_factor_auth_enabled": null,
        "signature": null,
        "details": null,
        "notes": null,
        "role_type": null,
        "custom_role_id": null,
        "moderator": true,
        "ticket_restriction": null,
        "only_private_comments": false,
        "restricted_agent": false,
        "suspended": false,
        "chat_only": false,
        "default_group_id": 360005501980,
        "report_csv": false,
        "user_fields": {}
    }
}

User class:

@Data
public class User {
    private long id;
    private String name;
    private String email;
    private String role;
}

UsersWrapper:

@Data
@AllArgsConstructor
public class UsersWrapper {
    private List<User> users = new ArrayList<>();

    public UsersWrapper() {
    }
}

Controller getUsers():

@GetMapping
    public List<User> getUsers() {

        UsersWrapper claims = new UsersWrapper();

        try {
            ResponseEntity<UsersWrapper> claimResponse = restTemplate.exchange(
                    resourceUrl,
                    HttpMethod.GET,
                    request(), // headers with basicAuth -> username:token
                    new ParameterizedTypeReference<UsersWrapper>() {
                    });

            if (claimResponse != null && claimResponse.hasBody()) {
                claims = claimResponse.getBody();
            }
        } catch (
                RestClientException e) {
            e.printStackTrace();
        }

        List<User> users = claims.getUsers();

        return users;
}

getUser():

@GetMapping("/{id}")
    public User getUser(@PathVariable long id) {

        String url = resourceUrl + "/" + id;
        ResponseEntity<User> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                request(), User.class);

        User user = response.getBody();
        return user;
}

Creating the following wrapper class with @JsonProperty("user") solved the case.

@Data
@AllArgsConstructor
public class UserWrapper {
    private UserDTO user;

    public UserWrapper() {
    }

    @JsonProperty("user")
    public UserDTO getUser() {
        return user;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM