【发布时间】:2019-08-30 12:15:30
【问题描述】:
我正在尝试使用带有分页功能的 Micronaut 控制器。 Micronaut-Data 有 this Spring inspired way 使用 Pageable 类访问存储库并返回 Page
当您要显示此分页数据时,问题就来了。我无法使用分页创建调用控制器。这里我有一个简单的控制器:
@Controller
public class PageableController {
private static final Logger LOGGER = LoggerFactory.getLogger(PageableController.class);
@Get(produces = APPLICATION_JSON, value = "/test{?pageable}")
public Page<String> getNames(@Nullable Pageable pageable) {
LOGGER.info("pageable {}", pageable);
if( pageable == null){
return Page.of(Arrays.asList("foo", "bar"), Pageable.UNPAGED, 2);
}else{
return Page.of(Arrays.asList("foo", "bar"), pageable, 2);
}
}
}
我希望能够用这样的方式调用它。但目前记录器显示可分页始终为空:
@MicronautTest
class PageableControllerTest {
@Inject
@Client("/")
private RxHttpClient client;
@Test
void callsWithPageable() {
String uri = "/test?size=20&number=2";
String orders = client.toBlocking().retrieve(HttpRequest.GET(uri));
//TODO, assert orders and pagination
}
如果我们可以用类似的东西来测试它会更好:
@Test
void callsWithPageableParsingJson() {
String uri = "/test?size=20&number=2";
//This fails to parse as it can't build pages.
Page<String> pages = client.toBlocking().retrieve(HttpRequest.GET(uri), pageOf(String.class));
assertThat(pages.getSize(), is(2));
assertThat(pages.getContent(), contains("foo", "bar"));
}
// Inspired by Argument.listOf
private static <T> Argument<Page<T>> pageOf(Class<T> type) {
return Argument.of((Class<Page<T>>) ((Class) Page.class), type);
}
这个Micronaut bug 表明正确的分页方式是使用 Micronaut Data
【问题讨论】:
标签: pagination controller micronaut