1.导入包:
import org.springframework.data.domain.Pageable;
2.测试类:
@Test
public void whenQuerySeccess() throws Exception{
mockMvc.perform(
get("/user")
.param("size","15")
.param("page","3")
.param("sort","age,desc")
.contentType(MediaType.APPLICATION_JSON_UTF8)
).andExpect(status().isOk())
.andExpect(jsonPath("$.length()").value(3))
.andReturn().getResponse().getContentAsString();
}
3.controller类:
@RestController
public class UserController {
@GetMapping("/user")
public List<User> query(Pageable pageable){
System.out.println(pageable.getPageSize());
System.out.println(pageable.getPageNumber());
System.out.println(pageable.getSort());
List<User> users = new ArrayList<>();
return users;
}
}
4.还可以传入默认的Pageable属性(使用@PageableDefault注解)
@RestController
public class UserController {
@GetMapping("/user")
public List<User> query(@PageableDefault(page=2,size=17,sort="username,asc")Pageable pageable){
System.out.println(pageable.getPageSize());
System.out.println(pageable.getPageNumber());
System.out.println(pageable.getSort());
List<User> users = new ArrayList<>();
return users;
}
}