服务提供者使用Pageable传递分页请求, 使用Page<T>传递分页结果
在接口中加入Pageable对象, mvc会接收如下参数: (所有参数均为可选项)
- Integer page
页码, 第一页为0 - Integer size
每页数据量 - List<String> sort
排序规则,字符串格式是 property[, direction], 例如 "datetime, DESC", 可以使用多值参数传递多个排序
可以使用@PageableDefault表明默认参数
@GetMapping("/countries")
public Page<Country> listCountry(@RequestParam(required = false) String continent,
@PageableDefault(page = 0, size = 10) Pageable pageable) {
如果想让swagger识别Pageable对象, 需要加入以下配置:
/**
* swagger不会自动解析Pageable, 需要加入以下配置
*/
@Configuration
public static class SpringDataConfiguration {
@Bean
public AlternateTypeRuleConvention pageableConvention(final TypeResolver resolver) {
return new AlternateTypeRuleConvention() {
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}
@Override
public List<AlternateTypeRule> rules() {
return newArrayList(AlternateTypeRules.newRule(resolver.resolve(Pageable.class), resolver.resolve(Page.class)));
}
};
}
@ApiModel
@Data
static class Page {
@ApiModelProperty(position = 0, value = "页码,第一页为0")
private Integer page;
@ApiModelProperty(position = 1, value = "每页的数据量")
private Integer size;
@ApiModelProperty(position = 2, value = "排序字段,可以是多个, 格式为\"{属性}[,{方向}]\"", example = "createDatetime, DESC")
private List<String> sort;
}
}
使用RestTemplate访问的时候, 无法解析Page或者PageImpl对象, 报错:
Can not construct instance of org.springframework.data.domain.Page: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
一个解决方法是继承一个带默认构造函数的具体类:
public class RestPageImpl<T> extends PageImpl<T> {
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public RestPageImpl(@JsonProperty("content") List<T> content,
@JsonProperty("number") int page,
@JsonProperty("size") int size,
@JsonProperty("totalElements") long total) {
super(content, new PageRequest(page, size), total);
}
public RestPageImpl(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public RestPageImpl(List<T> content) {
super(content);
}
public RestPageImpl() {
super(new ArrayList<T>());
}
}
并使用这个具体类来解析:
ResponseEntity<RestPageImpl<Country>> response = restTemplate.exchange(searchCountries, new ParameterizedTypeReference<RestPageImpl<Country>>() {
});
通过FeignClient传递Pageable还没有找到特别好的办法 ,选择直接传递刚才提到的几个参数(page,size, sort):
@GetMapping("/countries")
public RestPageImpl<Country> searchCountries(@RequestParam(required = false, name = "continent") String continent,
@RequestParam(required = false, name = "page") Integer page,
@RequestParam(required = false, name = "size") Integer size,
@RequestParam(required = false, name = "sort") List<String> sort
另外FeignClient也是不能解析Page接口, 也是继承一个具体类
调用端拆包Pageable:
@GetMapping("/search")
public Page<Country> listCountry(@RequestParam(required = false) String continent,
@PageableDefault(page = 0, size = 10) Pageable pageable) {
Page<Country> asia = countryService.searchCountries(continent, pageable.getPageNumber(), pageable.getPageSize(), convert(pageable.getSort()));
return asia;
}
default List<String> convert(Sort sort) {
if (sort == null) {
return null;
}
Iterator<Sort.Order> iterator = sort.iterator();
List<String> sortList = Stream.generate(sort.iterator()::next)
.map(order -> order.getProperty() + "," + order.getDirection())
.collect(Collectors.toList());
return sortList;
}