ResponseEntity 为 JSON 响应返回 null

我有一个 Restcontroller 类 -PolicyHoldersController- 向返回 JSON 对象的 API 发出 GET 请求。当我点击 RestController URL 时,我得到 null,虽然响应状态是 200。我相信我得到了一个有效负载,但我没有正确地将响应映射到我的 java bean -PolicyHolderBean,它复制了 JSON 响应的结构。


在 PolicyholdersControllers 中,我调用另一个控制器 -authorizationTokenController-,它也返回带有令牌的 JSON。它以与 PolicyHoldersControllerworks 相同的方式实现并且工作正常,所以我很困惑为什么我的浏览器中的 PolicyHoldersController 为空。我错过了什么吗?


PolicyHolders控制器:


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

    }

}


慕标5832272
浏览 361回答 2
2回答

犯罪嫌疑人X

使用 @JsonProperty 将输出字符串映射到您的 DTO ex:@JsonProperty("authorizationToken") 私有字符串 authToken;并继续在 ResponseEntity 中使用对象而不是 String&nbsp;ResponseEntity<SecurityToken>&nbsp;responseEntity&nbsp;=&nbsp;new&nbsp;RestTemplate().postForEntity( &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;endpoint, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;credentials, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SecurityToken.class &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);

HUWWW

它适用于方法的这种实现getPolicyHolders():@GetMapping("/policyholders")&nbsp; &nbsp; public PolicyHolderBean getPolicyHolders() {&nbsp; &nbsp; &nbsp; &nbsp; HttpEntity<String> request =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new HttpEntity<String>(setHeaders());&nbsp; &nbsp; &nbsp; &nbsp; ResponseEntity<String> responseEntity = new RestTemplate().exchange(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://api.sandbox.socotra.com/policyholders",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpMethod.GET,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String.class&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; String output = responseEntity.getBody();&nbsp; &nbsp; &nbsp; &nbsp; Gson gson=new Gson();&nbsp; &nbsp; &nbsp; &nbsp; PolicyHolderBean policyHolders=gson.fromJson(output,PolicyHolderBean.class);&nbsp; &nbsp; &nbsp; &nbsp; return policyHolders;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java