I am using Spring Data REST with Spring Boot (1.5.17) and I have the following controller in my code.
@RestController
public class StudentController {
@RequestMapping(method = GET, value = "students/{id}/notifications")
public @ResponseBody
ResponseEntity<?> getStudentNotifications(@PathVariable Long id, PersistentEntityResourceAssembler resourceAssembler, Pageable page) {
Student student = studentRepo.findOne(id);
Page<Notification> notifications = notificationHandler.getUnreadNotifications(student.getId(),page);
return new ResponseEntity<>(pagedResourcesAssembler.toResource(notifications, resourceAssembler), HttpStatus.OK);
}
}
The controller works correctly except I cannot use the sort parameter like students/1/notifications?sort=createdDate,DESC
. It always sorts by created date in ascending order.
I printed the Pageable parameter to console [number: 0, size 20, sort: null]
and it shows that the sort attribute is always null.
So what am I doing wrong here?
EDIT
MVC configuration
@Configuration
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
@Autowired
@Qualifier("repositoryExporterHandlerAdapter")
RequestMappingHandlerAdapter repositoryExporterHandlerAdapter;
@Override
public void addArgumentResolvers(
List<HandlerMethodArgumentResolver> argumentResolvers) {
List<HandlerMethodArgumentResolver> customArgumentResolvers = repositoryExporterHandlerAdapter.getCustomArgumentResolvers();
argumentResolvers.addAll(customArgumentResolvers);
}
@Override
public void addCorsMappings(CorsRegistry registry) {
}
}
The controller works correctly except I cannot use the sort parameter like students/1/notifications?createdDate,DESC. It always sorts by created date in ascending order.
if you are trying to pass the sort in
students/1/notifications?createdDate,DESC
it will not works because Pageable has sort params and you need to call like below.
students/1/notifications??sort=createdDate,DESC
在addArgumentResolvers
方法中添加以下内容
argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.