ResponseEntity对于JSON响应返回null

时间:2019-01-19 19:38:40

标签: java json spring http

我有一个Restcontroller类-PolicyHoldersController-,该类对返回JSON对象的API进行GET请求。当我点击RestController URL时,尽管响应状态为200,但是我还是得到了null。我相信我正在获得有效负载,但是我没有正确地将响应映射到我的Java bean -PolicyHolderBean,它复制了JSON响应的结构。

在PolicyholdersControllers中,我称为另一个控制器-authorizationTokenController-,该控制器还返回带有令牌的JSON。它的实现方式与PolicyHoldersControllerworks相同,并且可以正常工作,因此我很困惑为什么我在浏览器中无法获取PolicyHoldersController的null。我想念什么吗?

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;
    }
}

这是浏览器中的输出:

{
   "policyHolders": null
}

这是控制台输出:

: 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输出

{
    "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"
                ]
            }
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

它与方法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;

    }

答案 1 :(得分:0)

使用@JsonProperty与您的DTO ex映射输出字符串:

@JsonProperty(“ authorizationToken”) 私有字符串authToken;

,并继续使用ResponseEntity中的对象代替String

 ResponseEntity<SecurityToken> responseEntity = new RestTemplate().postForEntity(
                endpoint,
                credentials,
                SecurityToken.class
        );