Richard
Richard

Reputation: 6116

Spring Rest operations convert response to class

This is my rest call:

RestOperations operations;
UserIdentifier resourceResponse = operations.postForObject("...", entity, UserIdentifier.class);

The UserIdentifier class:

public class UserIdentifier {
    private String uid;
    private String code;
    private String retailId;

    public UserIdentifier() {

    }

    public UserIdentifier(String uid, String code, String retailId) {
        this.uid = uid;
        this.code = code;
        this.retailId = retailId;
    }

    public String getuid() {
        return uid;
    }

    public void setuid(String uid) {
        this.uid = uid;
    }

    public String getcode() {
        return code;
    }

    public void setcode(String code) {
        this.code = code;
    }

    public String getretailId() {
        return retailId;
    }

    public void setretailId(String retailId) {
        this.retailId = retailId;
    }
}

The JSON response:

{
  "userIdentifier": {
    "uid": "ee63a52cda7bf411dd8603ac196951aa77",
    "code": "63a5297e7bf411dd8603ac196951aa77",
    "retailId": "860658787",
    "pointOfEntry": "RETAIL"
  },
  "resultCode": true
}

But the problem is when i run this code, it creates the resourceResponse but all the fields are empty. Any idea what the problem is?

Edit 1: I am getting the proper json response back, i'm just unable to put that into a java class object.

Upvotes: 0

Views: 1639

Answers (1)

Nikson Kanti Paul
Nikson Kanti Paul

Reputation: 3440

It's failing to cast the response with class. According to your response json, your casting object should be like following

class Response {
    UserIdentifier userIdentifier;
    boolean resultCode;
    ...
}

and call would be

Response response = operations.postForObject("...", entity, Response.class);
response.userIdentifier...   // it is your data 

Upvotes: 2

Related Questions