目录
  1. 1. Pageable类源码
  2. 2. Swagger显示Pageable参数
    1. 2.0.0.1. 上篇文章为什么说要集成swagger2.8或以上,因为在这个配置类中如果低于2.8就会报错
解决使用Pageable在Swagger页面上不能传值问题

Pageable类源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public interface Pageable {

/**
* Returns the page to be returned.
*
* @return the page to be returned.
*/
int getPageNumber();

/**
* Returns the number of items to be returned.
*
* @return the number of items of that page
*/
int getPageSize();

/**
* Returns the offset to be taken according to the underlying page and page size.
*
* @return the offset to be taken
*/
int getOffset();

/**
* Returns the sorting parameters.
*
* @return
*/
Sort getSort();

/**
* Returns the {@link Pageable} requesting the next {@link Page}.
*
* @return
*/
Pageable next();

/**
* Returns the previous {@link Pageable} or the first {@link Pageable} if the current one already is the first one.
*
* @return
*/
Pageable previousOrFirst();

/**
* Returns the {@link Pageable} requesting the first page.
*
* @return
*/
Pageable first();

/**
* Returns whether there's a previous {@link Pageable} we can access from the current one. Will return
* {@literal false} in case the current {@link Pageable} already refers to the first page.
*
* @return
*/
boolean hasPrevious();
}

我们可以看出源码中我们所需的都已定义好了,我们只需使用。

第一种方法:

1
2
3
Sort sort = new Sort(Direction.DESC, "id"));
//生成Pageable变量
Pageable pageable = new PageRequest(pageNumber - 1, pageSize, sort);

pageNumber为什么要减个1呢,因为Pageable中第一页是从0开始的

第二种方法:

1
@PageableDefault(value = 10, sort = { "startTime" }, direction = Sort.Direction.ASC)Pageable pageable

推荐使用第二种使用注解加在Controller方法的参数中,像使用@RequestParam一样,这种方法避免了多余的代码,看上去清爽自然

Swagger显示Pageable参数

新增一个配置类,代码如下:

上篇文章为什么说要集成swagger2.8或以上,因为在这个配置类中如果低于2.8就会报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@Configuration
@SuppressWarnings("SpringJavaAutowiringInspection")
public 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(newRule(resolver.resolve(Pageable.class), resolver.resolve(Page.class)));
}
};
}

@ApiModel
static class Page {
@ApiModelProperty("Results page you want to retrieve (0..N)")
private Integer page;

@ApiModelProperty("Number of records per page")
private Integer size;

@ApiModelProperty("Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.")
private List<String> sort;

public Integer getPage() {
return page;
}

public void setPage(Integer page) {
this.page = page;
}

public Integer getSize() {
return size;
}

public void setSize(Integer size) {
this.size = size;
}

public List<String> getSort() {
return sort;
}

public void setSort(List<String> sort) {
this.sort = sort;
}
}
}

启动项目,页面访问Swagger地址就能见到效果了!!!

文章作者: 今晚早睡
文章链接: https://www.chensubao.com/article/32823.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 洛 苏り

评论