0

I have a Restcontroller class -PolicyHoldersController- that does a GET request to an API that returns a JSON object. When I hit the RestController URL, I get null, although the response status is 200. I believe that I am getting a payload but I am no properly mapping the response to my java bean -PolicyHolderBean, which replicates the structure of the JSON response.

In PolicyholdersControllers, I call another controller -authorizationTokenController- which also returns a JSON with a token. It is implemented in the same way as PolicyHoldersControllerworks and it works fine, so I am confused why I am getting null in my browser for PolicyHoldersController. Am I missing something?

PolicyHoldersController:

@RestController
public class PolicyHoldersController {

    private HttpHeaders setHeaders() {

         AuthorizationTokenController authorizationTokenController =
                new AuthorizationTokenController();

         AuthorizationTokenBean authorizationTokenBean =
                authorizationTokenController.postAuthorizationToken();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", authorizationTokenBean.getAuthorizationToken());
        headers.add("accept", "application/json");

         return headers;
    }

    @GetMapping("/policyholders")
    public PolicyHolderBean getPolicyHolders() {

        HttpEntity<String> request =
                new HttpEntity<String>(setHeaders());

        ResponseEntity<PolicyHolderBean> responseEntity = new RestTemplate().exchange(
                "https://api.acmeinsurance.com/policyholders",
                HttpMethod.GET,
                request,
                PolicyHolderBean.class
        );

        PolicyHolderBean policyholders = responseEntity.getBody();

        return policyholders;

    }

PolicyHolderBean:

public class PolicyHolderBean {

    private PolicyHolderResponse3Bean[] policyholders;

    public PolicyHolderBean() {

    }

    public PolicyHolderBean(PolicyHolderResponse3Bean[] policyholders) {
        this.policyholders = policyholders;
    }

    public PolicyHolderResponse3Bean[] getPolicyHolders() {
        return policyholders;
    }

    public void setPolicyHolders(PolicyHolderResponse3Bean[] policyholders) {
        this.policyholders = policyholders;
    }
}

PolicyHolderResponse3Bean:

public class PolicyHolderResponse3Bean {

    private int createdTimestamp;
    private EntityResponse2Bean entity;
    private String locator;
    private String[] policyLocators;
    private int updatedTimestamp;
    private int version;

    /* Constructors */
    public PolicyHolderResponse3Bean() {

    }

    public PolicyHolderResponse3Bean(int createdTimestamp,
                                     EntityResponse2Bean entity,
                                     String locator,
                                     String[] policyLocators,
                                     int updatedTimestamp,
                                     int version
    ) {
        this.createdTimestamp = createdTimestamp;
        this.entity = entity;
        this.locator = locator;
        this.policyLocators = policyLocators;
        this.updatedTimestamp = updatedTimestamp;
        this.version = version;
    }

    /* Getters */
    public int getCreatedTimestamp() {
        return createdTimestamp;
    }

    public EntityResponse2Bean getEntity() {
        return entity;
    }

    public String getLocator() {
        return locator;
    }

    public String[] getPolicyLocators() {
        return policyLocators;
    }

    public int getUpdatedTimestamp() {
        return updatedTimestamp;
    }

    public int getVersion() {
        return version;
    }

    /* Setters */
    public void setCreatedTimestamp(int createdTimestamp) {
        this.createdTimestamp = createdTimestamp;
    }

    public void setEntity(EntityResponse2Bean entity) {
        this.entity = entity;
    }

    public void setLocator(String locator) {
        this.locator = locator;
    }

    public void setPolicyLocators(String[] policyLocators) {
        this.policyLocators = policyLocators;
    }

    public void setUpdatedTimestamp(int updatedTimestamp) {
        this.updatedTimestamp = updatedTimestamp;
    }

    public void setVersion(int version) {
        this.version = version;
    }
}

EntityResponse2Bean:

public class EntityResponse2Bean {

    private String accountLocator;
    private boolean completed;
    private String createdTimestamp;
    private String locator;
    private List<EntityResponse2Bean> subEntitiesByLocator;
    private String updatedTimestamp;
    private Object values;

    /* Constructors */
    public EntityResponse2Bean() {

    }

    public EntityResponse2Bean(String accountLocator, boolean completed, String createdTimestamp, String locator, List<EntityResponse2Bean> subEntitiesByLocator, String updatedTimestamp, Object values) {
        this.accountLocator = accountLocator;
        this.completed = completed;
        this.createdTimestamp = createdTimestamp;
        this.locator = locator;
        this.subEntitiesByLocator = subEntitiesByLocator;
        this.updatedTimestamp = updatedTimestamp;
        this.values = values;
    }

    public String getAccountLocator() {
        return accountLocator;
    }

    public void setAccountLocator(String accountLocator) {
        this.accountLocator = accountLocator;
    }

    public boolean isCompleted() {
        return completed;
    }

    public void setCompleted(boolean completed) {
        this.completed = completed;
    }

    public String getCreatedTimestamp() {
        return createdTimestamp;
    }

    public void setCreatedTimestamp(String createdTimestamp) {
        this.createdTimestamp = createdTimestamp;
    }

    public String getLocator() {
        return locator;
    }

    public void setLocator(String locator) {
        this.locator = locator;
    }

    public List<EntityResponse2Bean> getSubEntitiesByLocator() {
        return subEntitiesByLocator;
    }

    public void setSubEntitiesByLocator(List<EntityResponse2Bean> subEntitiesByLocator) {
        this.subEntitiesByLocator = subEntitiesByLocator;
    }

    public String getUpdatedTimestamp() {
        return updatedTimestamp;
    }

    public void setUpdatedTimestamp(String updatedTimestamp) {
        this.updatedTimestamp = updatedTimestamp;
    }

    public Object getValues() {
        return values;
    }

    public void setValues(Object values) {
        this.values = values;
    }
}

This is the output in the browser:

{
   "policyHolders": null
}

This is the console output:

: HTTP GET https://api.acmeinsurance.com/policyholders
: Accept=[application/json, application/*+json]
: Response 200 OK
: Reading to [com.socotra.apimiddleware.PolicyHolderBean]
: Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/webp, image/apng, application/xml;q=0.9, */*;q=0.8] and supported    [application/json, application/*+json, application/json, application/*+json]
: Writing [com.acmeinsurance.PolicyHolderBean@7bb9a975]
: No view rendering, null ModelAndView returned.
: Completed 200 OK, headers={masked}
: Returning cached instance of singleton bean 'liveReloadServerEventListener'
: Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1cc456cb
: Bound request context to thread: org.apache.catalina.connector.RequestFacade@1cc456cb
: GET "/favicon.ico", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
: Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler [class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], ServletContext resource [/], class path resource []]] and 1 interceptors
: No view rendering, null ModelAndView returned.
: Completed 200 OK, headers={masked}
: Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1cc456cb

API output in postman

{
    "policyholders": [
        {
            "locator": "0282cc44-ba28-420d-90a7-e437bc1c2bef",
            "version": "2",
            "createdTimestamp": "1547761093737",
            "updatedTimestamp": "1547761119188",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "0282cc44-ba28-420d-90a7-e437bc1c2bef",
                "revision": "11e9-1aa0-40988e20-a822-0242ac110002",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1547761119188",
                "updatedTimestamp": "1547761119188",
                "values": {
                    "occupation": [
                        "Military"
                    ],
                    "first_name": [
                        "iguyg"
                    ],
                    "policyholder_id": [
                        "854397756"
                    ],
                    "last_name": [
                        "gigi"
                    ],
                    "marital_status": [
                        "Single"
                    ],
                    "gender": [
                        "Female"
                    ],
                    "date_of_birth": [
                        "1975-02-12"
                    ]
                },
                "completed": true,
                "flags": [
                    "flag.entity.workflow.complete"
                ]
            }
        },
        {
            "locator": "d46394fd-fb98-42b5-a5a5-7d57d67e45a9",
            "version": "1",
            "createdTimestamp": "1547753255083",
            "updatedTimestamp": "1547753255820",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "d46394fd-fb98-42b5-a5a5-7d57d67e45a9",
                "revision": "11e9-1a8d-f13257b0-8624-0242ac110002",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1547753255083",
                "updatedTimestamp": "1547753255083",
                "values": {
                    "policyholder_id": [
                        "467711991"
                    ]
                },
                "completed": false,
                "flags": []
            }
        },
        {
            "locator": "e7f20031-0fbe-489d-9217-64f886b58caa",
            "version": "2",
            "createdTimestamp": "1546699334507",
            "updatedTimestamp": "1546888656274",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "e7f20031-0fbe-489d-9217-64f886b58caa",
                "revision": "11e9-12b0-e42e3b90-82c2-0242ac11000c",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1546888656274",
                "updatedTimestamp": "1546888656274",
                "values": {
                    "occupation": [
                        "Military"
                    ],
                    "first_name": [
                        "Paco"
                    ],
                    "policyholder_id": [
                        "276877294"
                    ],
                    "last_name": [
                        "Romerales"
                    ],
                    "marital_status": [
                        "Common-in-Law"
                    ],
                    "gender": [
                        "Male"
                    ],
                    "date_of_birth": [
                        "1970-06-30"
                    ]
                },
                "completed": true,
                "flags": [
                    "flag.entity.workflow.complete"
                ]
            }
        },
        {
            "locator": "b6aa4dff-e02e-45d3-b06b-b47fef6cb073",
            "version": "1",
            "createdTimestamp": "1546693107318",
            "updatedTimestamp": "1546693107369",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "b6aa4dff-e02e-45d3-b06b-b47fef6cb073",
                "revision": "11e9-10e9-97e32160-82c2-0242ac11000c",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1546693107318",
                "updatedTimestamp": "1546693107318",
                "values": {
                    "occupation": [
                        "Unemployed"
                    ],
                    "first_name": [
                        "Diego"
                    ],
                    "last_name": [
                        "Farias"
                    ],
                    "marital_status": [
                        "Widower"
                    ],
                    "gender": [
                        "Male"
                    ],
                    "date_of_birth": [
                        "1979-05-25"
                    ]
                },
                "completed": true,
                "flags": [
                    "flag.entity.workflow.complete"
                ]
            }
        },
        {
            "locator": "5b9e2e89-0265-4b7a-9981-3b81e3ee35d5",
            "version": "1",
            "createdTimestamp": "1546692621948",
            "updatedTimestamp": "1546692622006",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "5b9e2e89-0265-4b7a-9981-3b81e3ee35d5",
                "revision": "11e9-10e8-76958bc0-82c2-0242ac11000c",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1546692621948",
                "updatedTimestamp": "1546692621948",
                "values": {
                    "policyholder_id": [
                        "546676476"
                    ]
                },
                "completed": false,
                "flags": []
            }
        },
        {
            "locator": "dce877ab-c21e-432a-9997-39e3e356c3ca",
            "version": "2",
            "createdTimestamp": "1546535435129",
            "updatedTimestamp": "1546535537410",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "dce877ab-c21e-432a-9997-39e3e356c3ca",
                "revision": "11e9-0f7a-b8ef7b80-82c2-0242ac11000c",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1546535537410",
                "updatedTimestamp": "1546535537410",
                "values": {
                    "occupation": [
                        "Medical"
                    ],
                    "first_name": [
                        "Emiliana"
                    ],
                    "policyholder_id": [
                        "257740828"
                    ],
                    "last_name": [
                        "Nosoque"
                    ],
                    "marital_status": [
                        "Common-in-Law"
                    ],
                    "gender": [
                        "Female"
                    ],
                    "date_of_birth": [
                        "1990-04-27"
                    ]
                },
                "completed": true,
                "flags": [
                    "flag.entity.workflow.complete"
                ]
            }
        },
        {
            "locator": "0dfb5db3-365b-424b-8df3-85e5cb1a0383",
            "version": "1",
            "createdTimestamp": "1546529777930",
            "updatedTimestamp": "1546529777980",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "0dfb5db3-365b-424b-8df3-85e5cb1a0383",
                "revision": "11e9-0f6d-4ffc56a0-998c-0242ac110002",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1546529777930",
                "updatedTimestamp": "1546529777930",
                "values": {
                    "first_name": [
                        "Pepito"
                    ],
                    "last_name": [
                        "Melenas"
                    ]
                },
                "completed": true,
                "flags": [
                    "flag.entity.workflow.complete"
                ]
            }
        },
        {
            "locator": "1d5d1fd8-a031-4131-9030-8b94a48e8b2b",
            "version": "2",
            "createdTimestamp": "1546446785969",
            "updatedTimestamp": "1546446932421",
            "policyAccounts": [],
            "policyLocators": [],
            "payments": [],
            "entity": {
                "locator": "1d5d1fd8-a031-4131-9030-8b94a48e8b2b",
                "revision": "11e9-0eac-6c3ece90-998c-0242ac110002",
                "accountLocator": "432f222e-dd0d-42ce-88b4-42ae06579e0f",
                "createdTimestamp": "1546446932421",
                "updatedTimestamp": "1546446932421",
                "values": {
                    "occupation": [
                        "Business Professional"
                    ],
                    "first_name": [
                        "Testcustomer"
                    ],
                    "policyholder_id": [
                        "464638311"
                    ],
                    "last_name": [
                        "lastname"
                    ],
                    "marital_status": [
                        "Married"
                    ],
                    "gender": [
                        "Male"
                    ],
                    "date_of_birth": [
                        "1990-03-07"
                    ]
                },
                "completed": true,
                "flags": [
                    "flag.entity.workflow.complete"
                ]
            }
        }
    ]
}
EMC
  • 95
  • 3
  • 11
  • update here and tag me – Ryuzaki L Jan 19 '19 at 22:07
  • @Deadpool. Postman response uploaded in the question body. I am using com.fasterxml.jackson.core jackson-databind – EMC Jan 19 '19 at 22:10
  • Three mistakes `version`, `createdTimestamp`,`updatedTimestamp` should be `string` not `int`, check data types in all POJO classes, i believe there are lot of mistakes, 2) on every property use `JsonProperty("") annotation that exactly matches the name in `JSON` output https://stackoverflow.com/questions/12583638/when-is-the-jsonproperty-property-used-and-what-is-it-used-for – Ryuzaki L Jan 19 '19 at 22:10
  • have you used `@JsonProperty`? and show this class `EntityResponse2Bean` update in the post also so that i can see – Ryuzaki L Jan 19 '19 at 22:20
  • @Deadpool, please check the answer I posted. It works. – EMC Jan 20 '19 at 06:26

2 Answers2

0

It works with this implementation of method getPolicyHolders():

@GetMapping("/policyholders")
    public PolicyHolderBean getPolicyHolders() {

        HttpEntity<String> request =
                new HttpEntity<String>(setHeaders());

        ResponseEntity<String> responseEntity = new RestTemplate().exchange(
                "https://api.sandbox.socotra.com/policyholders",
                HttpMethod.GET,
                request,
                String.class
        );

        String output = responseEntity.getBody();

        Gson gson=new Gson();
        PolicyHolderBean policyHolders=gson.fromJson(output,PolicyHolderBean.class);

        return policyHolders;

    }
EMC
  • 95
  • 3
  • 11
0

Use @JsonProperty to map the output string with your DTO ex:

@JsonProperty("authorizationToken") private String authToken;

and continue using objects in ResponseEntity instead of String

 ResponseEntity<SecurityToken> responseEntity = new RestTemplate().postForEntity(
                endpoint,
                credentials,
                SecurityToken.class
        );
Amit Pamecha
  • 119
  • 5