spring-cloud傳遞分頁查詢

服務(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末炭序,一起剝皮案震驚了整個(gè)濱河市恃慧,隨后出現(xiàn)的幾起案子越除,更是在濱河造成了極大的恐慌陶夜,老刑警劉巖卷扮,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異碎捺,居然都是意外死亡彤枢,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進(jìn)店門固翰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狼纬,“玉大人,你說我怎么就攤上這事骂际×屏穑” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵歉铝,是天一觀的道長盈简。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么柠贤? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任香浩,我火速辦了婚禮,結(jié)果婚禮上臼勉,老公的妹妹穿的比我還像新娘邻吭。我一直安慰自己,他們只是感情好宴霸,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布囱晴。 她就那樣靜靜地躺著,像睡著了一般瓢谢。 火紅的嫁衣襯著肌膚如雪畸写。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天氓扛,我揣著相機(jī)與錄音艺糜,去河邊找鬼。 笑死幢尚,一個(gè)胖子當(dāng)著我的面吹牛破停,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播尉剩,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼真慢,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了理茎?” 一聲冷哼從身側(cè)響起黑界,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎皂林,沒想到半個(gè)月后朗鸠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡础倍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年烛占,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沟启。...
    茶點(diǎn)故事閱讀 39,991評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡忆家,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出德迹,到底是詐尸還是另有隱情芽卿,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布胳搞,位于F島的核電站卸例,受9級特大地震影響称杨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜筷转,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一列另、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧旦装,春花似錦页衙、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至呻袭,卻和暖如春眨八,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背左电。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工廉侧, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人篓足。 一個(gè)月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓段誊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親栈拖。 傳聞我的和親對象是個(gè)殘疾皇子连舍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)涩哟,斷路器索赏,智...
    卡卡羅2017閱讀 134,657評論 18 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法贴彼,內(nèi)部類的語法潜腻,繼承相關(guān)的語法,異常的語法器仗,線程的語...
    子非魚_t_閱讀 31,632評論 18 399
  • 1融涣、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,981評論 3 119
  • Hello!大家好~~星星王子——《生日書》又與你見面啦青灼!★★這周我們來研究5月21日—5月27日這七天當(dāng)中出生的...
    璞學(xué)苑閱讀 1,119評論 0 0