服務(wù)提供者使用Pageable傳遞分頁請求, 使用Page<T>傳遞分頁結(jié)果
在接口中加入Pageable對象, mvc會接收如下參數(shù): (所有參數(shù)均為可選項(xiàng))
- Integer page
頁碼, 第一頁為0 - Integer size
每頁數(shù)據(jù)量 - List<String> sort
排序規(guī)則,字符串格式是 property[, direction], 例如 "datetime, DESC", 可以使用多值參數(shù)傳遞多個(gè)排序
可以使用@PageableDefault表明默認(rèn)參數(shù)
@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 = "每頁的數(shù)據(jù)量")
private Integer size;
@ApiModelProperty(position = 2, value = "排序字段,可以是多個(gè), 格式為\"{屬性}[,{方向}]\"", example = "createDatetime, DESC")
private List<String> sort;
}
}
使用RestTemplate訪問的時(shí)候, 無法解析Page或者PageImpl對象, 報(bào)錯(cuò):
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
一個(gè)解決方法是繼承一個(gè)帶默認(rèn)構(gòu)造函數(shù)的具體類:
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>());
}
}
并使用這個(gè)具體類來解析:
ResponseEntity<RestPageImpl<Country>> response = restTemplate.exchange(searchCountries, new ParameterizedTypeReference<RestPageImpl<Country>>() {
});
通過FeignClient傳遞Pageable還沒有找到特別好的辦法 ,選擇直接傳遞剛才提到的幾個(gè)參數(shù)(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接口, 也是繼承一個(gè)具體類
調(diào)用端拆包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;
}
測試代碼項(xiàng)目見 https://gitee.com/liuxy9999/mytest-spring-cloud