我有一个控制器定义了以下方法。
Mono<EntityModel<Some>> readSome(...) {
// works, simply.
}
Flux<EntityModel<Other>> readOther(...) {
// works, simply.
}
并且,在我的测试类中,我定义了以下有效的方法。
// WORKS!
static List<EntityModel<Some>> readSome(...) {
client...
.exchange()
.expectStatus().isOk()
.expectBodyList(new TypeReferences.EntityModelType<Some>() {
})
.returnResult()
.getResponseBody();
}
// WORKS!
static List<EntityModel<Other>> readOther(...) {
client...
.exchange()
.expectStatus().isOk()
.expectBodyList(new TypeReferences.EntityModelType<Other>() {
})
.returnResult()
.getResponseBody()
}
我自然而然地为所有类型定义了以下方法。
如您所见,它只是所有类型的通用方法。
private static <T> List<EntityModel<T>> readList(final WebTestClient client,
final Function<UriBuilder, URI> uriFunction,
@Nullable final String accept) {
final var responseBody = client
.get()
.uri(uriFunction)
.headers(h -> {
Optional.ofNullable(accept)
.map(MediaType::valueOf)
.map(List::of)
.ifPresent(h::setAccept);
})
.exchange()
.expectStatus().isOk()
.expectBodyList(new TypeReferences.EntityModelType<T>() { // NOT <T>, BUT Map
})
.returnResult()
.getResponseBody();
return Objects.requireNonNull(responseBody, "responseBody is null");
}
现在,当我用该方法改变readSome
|方法时,我看到了一种奇怪的行为。readOther
readList
Jackson 不再将内容解析为,而是解析为LinkedHashMap
。
我该如何使用Class<T>
来指定TypeReferences.EntityModelType<T>
?