Spring Data REST “findBy…” not sorting












0















I have a Spring Boot API (using 2.0.5.RELEASE spring-boot-starter-parent) and I'm using the spring-boot-starter-rest package to generate the endpoints for my API. In one of the repositories I have the following method:



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRack(@Param("rack") Rack rack);
}


Which exposes an endpoint at http://{base}/api/v1/rackPositions/findByRack



If I then call




http://{base}/api/v1/rackPositions/findByRack?rack={rack
url}&sort=positionNumber,asc




in Postman, the list returned isn't sorted. 'positionNumber' is a property on the RackPosition entity.



However if I change the repository method to



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRackOrderByPositionNumberAsc(@Param("rack") Rack rack);
}


and then call




http://{base}/api/v1/rackPositions/findByRackOrderByPositionNumberAsc?rack={rack
url}




it works fine. Is there a reason why the sort parameter doesn't work?










share|improve this question























  • I have always used the findBy...OrderBy.. interfaces - where have you seem that a sort parameter would work?

    – Scary Wombat
    Nov 16 '18 at 1:40











  • try changing http://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc to http://{base}/api/v1/rackPositions/search/rack?rack={rack url}&sort=positionNumber,asc

    – Kartik
    Nov 16 '18 at 1:43











  • @ScaryWombat it works

    – Kartik
    Nov 16 '18 at 1:44






  • 1





    @MatthewSatti As described at the bottom of this page, you shouldn't use the findBy part in the URL, and the structure of the URL looks a bit different

    – Kartik
    Nov 16 '18 at 1:45











  • @Kartik Glad I can come here and learn

    – Scary Wombat
    Nov 16 '18 at 1:46
















0















I have a Spring Boot API (using 2.0.5.RELEASE spring-boot-starter-parent) and I'm using the spring-boot-starter-rest package to generate the endpoints for my API. In one of the repositories I have the following method:



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRack(@Param("rack") Rack rack);
}


Which exposes an endpoint at http://{base}/api/v1/rackPositions/findByRack



If I then call




http://{base}/api/v1/rackPositions/findByRack?rack={rack
url}&sort=positionNumber,asc




in Postman, the list returned isn't sorted. 'positionNumber' is a property on the RackPosition entity.



However if I change the repository method to



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRackOrderByPositionNumberAsc(@Param("rack") Rack rack);
}


and then call




http://{base}/api/v1/rackPositions/findByRackOrderByPositionNumberAsc?rack={rack
url}




it works fine. Is there a reason why the sort parameter doesn't work?










share|improve this question























  • I have always used the findBy...OrderBy.. interfaces - where have you seem that a sort parameter would work?

    – Scary Wombat
    Nov 16 '18 at 1:40











  • try changing http://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc to http://{base}/api/v1/rackPositions/search/rack?rack={rack url}&sort=positionNumber,asc

    – Kartik
    Nov 16 '18 at 1:43











  • @ScaryWombat it works

    – Kartik
    Nov 16 '18 at 1:44






  • 1





    @MatthewSatti As described at the bottom of this page, you shouldn't use the findBy part in the URL, and the structure of the URL looks a bit different

    – Kartik
    Nov 16 '18 at 1:45











  • @Kartik Glad I can come here and learn

    – Scary Wombat
    Nov 16 '18 at 1:46














0












0








0








I have a Spring Boot API (using 2.0.5.RELEASE spring-boot-starter-parent) and I'm using the spring-boot-starter-rest package to generate the endpoints for my API. In one of the repositories I have the following method:



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRack(@Param("rack") Rack rack);
}


Which exposes an endpoint at http://{base}/api/v1/rackPositions/findByRack



If I then call




http://{base}/api/v1/rackPositions/findByRack?rack={rack
url}&sort=positionNumber,asc




in Postman, the list returned isn't sorted. 'positionNumber' is a property on the RackPosition entity.



However if I change the repository method to



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRackOrderByPositionNumberAsc(@Param("rack") Rack rack);
}


and then call




http://{base}/api/v1/rackPositions/findByRackOrderByPositionNumberAsc?rack={rack
url}




it works fine. Is there a reason why the sort parameter doesn't work?










share|improve this question














I have a Spring Boot API (using 2.0.5.RELEASE spring-boot-starter-parent) and I'm using the spring-boot-starter-rest package to generate the endpoints for my API. In one of the repositories I have the following method:



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRack(@Param("rack") Rack rack);
}


Which exposes an endpoint at http://{base}/api/v1/rackPositions/findByRack



If I then call




http://{base}/api/v1/rackPositions/findByRack?rack={rack
url}&sort=positionNumber,asc




in Postman, the list returned isn't sorted. 'positionNumber' is a property on the RackPosition entity.



However if I change the repository method to



@Repository
public interface RackPositionDao extends JpaRepository<RackPosition, String> {
List<RackPosition> findByRackOrderByPositionNumberAsc(@Param("rack") Rack rack);
}


and then call




http://{base}/api/v1/rackPositions/findByRackOrderByPositionNumberAsc?rack={rack
url}




it works fine. Is there a reason why the sort parameter doesn't work?







java spring spring-boot spring-data-rest






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 1:20









Matthew SattiMatthew Satti

287




287













  • I have always used the findBy...OrderBy.. interfaces - where have you seem that a sort parameter would work?

    – Scary Wombat
    Nov 16 '18 at 1:40











  • try changing http://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc to http://{base}/api/v1/rackPositions/search/rack?rack={rack url}&sort=positionNumber,asc

    – Kartik
    Nov 16 '18 at 1:43











  • @ScaryWombat it works

    – Kartik
    Nov 16 '18 at 1:44






  • 1





    @MatthewSatti As described at the bottom of this page, you shouldn't use the findBy part in the URL, and the structure of the URL looks a bit different

    – Kartik
    Nov 16 '18 at 1:45











  • @Kartik Glad I can come here and learn

    – Scary Wombat
    Nov 16 '18 at 1:46



















  • I have always used the findBy...OrderBy.. interfaces - where have you seem that a sort parameter would work?

    – Scary Wombat
    Nov 16 '18 at 1:40











  • try changing http://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc to http://{base}/api/v1/rackPositions/search/rack?rack={rack url}&sort=positionNumber,asc

    – Kartik
    Nov 16 '18 at 1:43











  • @ScaryWombat it works

    – Kartik
    Nov 16 '18 at 1:44






  • 1





    @MatthewSatti As described at the bottom of this page, you shouldn't use the findBy part in the URL, and the structure of the URL looks a bit different

    – Kartik
    Nov 16 '18 at 1:45











  • @Kartik Glad I can come here and learn

    – Scary Wombat
    Nov 16 '18 at 1:46

















I have always used the findBy...OrderBy.. interfaces - where have you seem that a sort parameter would work?

– Scary Wombat
Nov 16 '18 at 1:40





I have always used the findBy...OrderBy.. interfaces - where have you seem that a sort parameter would work?

– Scary Wombat
Nov 16 '18 at 1:40













try changing http://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc to http://{base}/api/v1/rackPositions/search/rack?rack={rack url}&sort=positionNumber,asc

– Kartik
Nov 16 '18 at 1:43





try changing http://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc to http://{base}/api/v1/rackPositions/search/rack?rack={rack url}&sort=positionNumber,asc

– Kartik
Nov 16 '18 at 1:43













@ScaryWombat it works

– Kartik
Nov 16 '18 at 1:44





@ScaryWombat it works

– Kartik
Nov 16 '18 at 1:44




1




1





@MatthewSatti As described at the bottom of this page, you shouldn't use the findBy part in the URL, and the structure of the URL looks a bit different

– Kartik
Nov 16 '18 at 1:45





@MatthewSatti As described at the bottom of this page, you shouldn't use the findBy part in the URL, and the structure of the URL looks a bit different

– Kartik
Nov 16 '18 at 1:45













@Kartik Glad I can come here and learn

– Scary Wombat
Nov 16 '18 at 1:46





@Kartik Glad I can come here and learn

– Scary Wombat
Nov 16 '18 at 1:46












3 Answers
3






active

oldest

votes


















0














Please follow this :
https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html



<S extends T> List<S> findAll(Example<S> example, Sort sort)





share|improve this answer

































    0














    JpaRepository interface extends PagingAndSortingRepository interface.



    So, you can use endpoint as
    http://{base}/api/v1/rackPositions/findbyRack or http://{base}/api/v1/rackPositions/find-byRack

    by avoiding camel caps.






    share|improve this answer

































      0














      There are 2 ways to sort result in Spring data JPA





      1. Using Custom JPA Query as below



        @Query("Select r from rack order by position_number ASC")



      2. Using JPA custom methods (as you used : findByRackOrderByPositionNumberAsc())



      3. Passing Sort object in repository method as below



        List<RackPosition> findByRack(@Param("rack") Rack rack,org.springframework.data.domain.Sort sort);




      and call the method as below



      Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
      Object obj = repo.findByRack(rackObject, sort);





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330135%2fspring-data-rest-findby-not-sorting%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Please follow this :
        https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html



        <S extends T> List<S> findAll(Example<S> example, Sort sort)





        share|improve this answer






























          0














          Please follow this :
          https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html



          <S extends T> List<S> findAll(Example<S> example, Sort sort)





          share|improve this answer




























            0












            0








            0







            Please follow this :
            https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html



            <S extends T> List<S> findAll(Example<S> example, Sort sort)





            share|improve this answer















            Please follow this :
            https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html



            <S extends T> List<S> findAll(Example<S> example, Sort sort)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 4:33

























            answered Nov 16 '18 at 4:25









            darshakatdarshakat

            391111




            391111

























                0














                JpaRepository interface extends PagingAndSortingRepository interface.



                So, you can use endpoint as
                http://{base}/api/v1/rackPositions/findbyRack or http://{base}/api/v1/rackPositions/find-byRack

                by avoiding camel caps.






                share|improve this answer






























                  0














                  JpaRepository interface extends PagingAndSortingRepository interface.



                  So, you can use endpoint as
                  http://{base}/api/v1/rackPositions/findbyRack or http://{base}/api/v1/rackPositions/find-byRack

                  by avoiding camel caps.






                  share|improve this answer




























                    0












                    0








                    0







                    JpaRepository interface extends PagingAndSortingRepository interface.



                    So, you can use endpoint as
                    http://{base}/api/v1/rackPositions/findbyRack or http://{base}/api/v1/rackPositions/find-byRack

                    by avoiding camel caps.






                    share|improve this answer















                    JpaRepository interface extends PagingAndSortingRepository interface.



                    So, you can use endpoint as
                    http://{base}/api/v1/rackPositions/findbyRack or http://{base}/api/v1/rackPositions/find-byRack

                    by avoiding camel caps.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 16 '18 at 5:50









                    Kartik

                    4,29731537




                    4,29731537










                    answered Nov 16 '18 at 4:40









                    WGSSAMINTHAWGSSAMINTHA

                    10210




                    10210























                        0














                        There are 2 ways to sort result in Spring data JPA





                        1. Using Custom JPA Query as below



                          @Query("Select r from rack order by position_number ASC")



                        2. Using JPA custom methods (as you used : findByRackOrderByPositionNumberAsc())



                        3. Passing Sort object in repository method as below



                          List<RackPosition> findByRack(@Param("rack") Rack rack,org.springframework.data.domain.Sort sort);




                        and call the method as below



                        Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
                        Object obj = repo.findByRack(rackObject, sort);





                        share|improve this answer




























                          0














                          There are 2 ways to sort result in Spring data JPA





                          1. Using Custom JPA Query as below



                            @Query("Select r from rack order by position_number ASC")



                          2. Using JPA custom methods (as you used : findByRackOrderByPositionNumberAsc())



                          3. Passing Sort object in repository method as below



                            List<RackPosition> findByRack(@Param("rack") Rack rack,org.springframework.data.domain.Sort sort);




                          and call the method as below



                          Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
                          Object obj = repo.findByRack(rackObject, sort);





                          share|improve this answer


























                            0












                            0








                            0







                            There are 2 ways to sort result in Spring data JPA





                            1. Using Custom JPA Query as below



                              @Query("Select r from rack order by position_number ASC")



                            2. Using JPA custom methods (as you used : findByRackOrderByPositionNumberAsc())



                            3. Passing Sort object in repository method as below



                              List<RackPosition> findByRack(@Param("rack") Rack rack,org.springframework.data.domain.Sort sort);




                            and call the method as below



                            Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
                            Object obj = repo.findByRack(rackObject, sort);





                            share|improve this answer













                            There are 2 ways to sort result in Spring data JPA





                            1. Using Custom JPA Query as below



                              @Query("Select r from rack order by position_number ASC")



                            2. Using JPA custom methods (as you used : findByRackOrderByPositionNumberAsc())



                            3. Passing Sort object in repository method as below



                              List<RackPosition> findByRack(@Param("rack") Rack rack,org.springframework.data.domain.Sort sort);




                            and call the method as below



                            Sort sort = new Sort(new Sort.Order(Direction.ASC, "lastName"));
                            Object obj = repo.findByRack(rackObject, sort);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 7:43









                            TheSprinterTheSprinter

                            573316




                            573316






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330135%2fspring-data-rest-findby-not-sorting%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Florida Star v. B. J. F.

                                Danny Elfman

                                Lugert, Oklahoma