Issue
I am using Spring JPA DATA in my project. I wanna make my data pageble. I have already add Pageable to my repository method. But it shows error something like that:
Request processing failed; nested exception is java.lang.IllegalArgumentException: You have to provide at least one property to sort by! java.lang.IllegalArgumentException: You have to provide at least one property to sort by! org.springframework.data.domain.Sort.<init>(Sort.java:92) org.springframework.data.domain.Sort.<init>(Sort.java:80) org.springframework.data.domain.PageRequest.<init>(PageRequest.java:52) com.datum.fnd.controller.rest.NodeRestController.getNodes(NodeRestController.java:44) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method).....
I want to share my codes with you. Please look over following codes. I haven't find where I did mistake
My @Repository class:
@Query(value = "select new com.datum.fnd.domain.Node(c.idNode, c.name,c.address, c.description, c.point) from Node c")
List<Node> getNodesByPage(Pageable pageable);
My Service class:
public interface NodeService extends DefaultService<Node, Long> {
List<Node> getUnboundedNodes();
List<Node> getNodes(Pageable pageable);
}
My ServiceImpl class:
@Service
public class NodeServiceImpl implements NodeService {
private static final Logger LOG = Logger.getLogger(NodeServiceImpl.class);
@Autowired
private NodeRepository nodeRepository;
@Autowired
private ChannelNodeRepository channelNodeRepository;
@Override
public Node create(Node node) {
return nodeRepository.save(node);
}
@Transactional(readOnly = true)
@Override
public List<Node> list() {
return nodeRepository.selectAll();
}
@Transactional(readOnly = true)
@Override
public List<Node> getNodes(Pageable pageable) {
return nodeRepository.getNodesByPage(pageable);
}
My Controller class:
@RequestMapping(value = "/page/{last_item}", method = RequestMethod.GET)
public List<Node> getNodes(@PathVariable(value = "last_item") int last_item) {
LOG.info("Retrieve all nodes");
PageRequest page_req = new PageRequest(0, last_item, Direction.DESC);
return nodeService.getNodes(page_req);
}
And when I try to call "http://localhost:8088/FNDWEB/rest/node/page/50" it shows the above-mentioned error.
Solution
Exception message and documentation of PageRequest should make your problem clear. If you provide Sort.Direction
to PageRequest
constructor, you have to provide also at least one property name (according to which entities should be sorted).
For example:
PageRequest page_req = new PageRequest(0, last_item, Direction.DESC, "idNode");
Answered By - Jakub Ch.
Answer Checked By - Katrina (JavaFixing Volunteer)