0

请参阅下面的代码,我正在尝试将结果转换为 Profile 类型模型并对其进行迭代以获取该字段。但是我得到了类转换异常。请参阅下面附加的示例响应,需要从结果中获取 pid。

//code

    // search profile by profileId
    SearchCriteria sc = new SearchCriteria(Profile.PROFILE_ID, EQUALS, profileId);
    SearchInput searchInput = SearchInput.builder().criteria(sc).build();


  HttpEntity<SearchInput> searchRequest = new HttpEntity<>(searchInput);
            PageableResponse response = restTemplate.postForObject(searchUrl, searchRequest, PageableResponse.class);
            Object results =  response.getResults();
            ArrayList<Profile>  profiles =  (ArrayList<Profile>) results;
            System.out.println(profiles.get(0));
            for(Profile p: profiles) {
            log.trace(p.getId());   
            }   
        }

错误日志:

java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.model.profile.Profile (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.palmtree.matrimony.model.profile.Profile is in unnamed module of loader 'app')
        at com..integration.ProfileSearchIntegrationTest.searchProfileByProfileId(ProfileSearchIntegrationTest.java:97)
    

// 样本响应

{
    "total": 1095,
    "page": 0,
    "size": 50,
    "results": [
        {
            "id": "_AGi3ncBn8p1nefEm0Oe",
            "cdt": "2021-02-26T13:58:54.563Z",
            "mdt": "2021-02-26T13:58:54.563Z",
            "pid": 12038422,
            "ps": "Draft"...}]
4

1 回答 1

1

我将其重新发布为答案,因为它可能有助于 OP 解决问题。

您可以尝试使用方法并作为类型restTemplate.exchange()传入. 我认为你不应该使用 PageableResponse 作为返回类型,因为你没有在这个测试中测试分页。PageableResponse<Profile>ParameterizedTypeReference

于 2021-03-04T06:44:50.500 回答