我正在使用以下内容从后端服务返回一页对象:
@JsonIgnoreProperties(ignoreUnknown = true, value = {"pageable"})
class RestPage<Role> extends PageImpl<Role> {
private static final Logger LOG = LoggerFactory.getLogger(RestPage.class);
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestPage(@JsonProperty("content") List<Role> content,
@JsonProperty("number") int page,
@JsonProperty("size") int size,
@JsonProperty("totalElements") long total,
@JsonProperty("numberOfElements") int numberOfElements,
@JsonProperty("pageNumber") int pageNumber
) {
super(content, PageRequest.of(page, size), total);
}
public RestPage(Page<Role> page) {
super(page.getContent(), page.getPageable(), page.getTotalElements());
LOG.info("page.content: {}", page.getContent());
}
}
当我从以下http请求中获取页面并尝试使用以下代码通过带有列表的流时:
EntityExchangeResult<RestPage> entityExchangeResult = webTestClient.get().uri("/roles/"+organizationId)
.headers(addJwt(jwt)).exchange().expectStatus().isOk().expectBody(RestPage.class)
.returnResult();
LOG.info("roles found: {}", entityExchangeResult.getResponseBody().getContent());
List<Role> list = entityExchangeResult.getResponseBody().getContent();
List<String> listOfNames = list.stream().map(Role::getName).collect(toList());
我在
map()
功能行:
class java.util.LinkedHashMap cannot be cast to class my.package.repo.entity.Role
是否有方法将ObjectMapper调用放入
RestPage
转换为我想要的对象类型?我想得到我的特定类型,而不是我目前看到的LinkedHashMap。