Spring Data REST “findBy…” not sorting
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
|
show 7 more comments
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
I have always used thefindBy...OrderBy..
interfaces - where have you seem that asort
parameter would work?
– Scary Wombat
Nov 16 '18 at 1:40
try changinghttp://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc
tohttp://{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 thefindBy
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
|
show 7 more comments
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
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
java spring spring-boot spring-data-rest
asked Nov 16 '18 at 1:20
Matthew SattiMatthew Satti
287
287
I have always used thefindBy...OrderBy..
interfaces - where have you seem that asort
parameter would work?
– Scary Wombat
Nov 16 '18 at 1:40
try changinghttp://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc
tohttp://{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 thefindBy
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
|
show 7 more comments
I have always used thefindBy...OrderBy..
interfaces - where have you seem that asort
parameter would work?
– Scary Wombat
Nov 16 '18 at 1:40
try changinghttp://{base}/api/v1/rackPositions/findByRack?rack={rack url}&sort=positionNumber,asc
tohttp://{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 thefindBy
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
|
show 7 more comments
3 Answers
3
active
oldest
votes
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)
add a comment |
JpaRepository interface extends PagingAndSortingRepository interface.
So, you can use endpoint ashttp://{base}/api/v1/rackPositions/findbyRack
or http://{base}/api/v1/rackPositions/find-byRack
by avoiding camel caps.
add a comment |
There are 2 ways to sort result in Spring data JPA
Using Custom JPA Query as below
@Query("Select r from rack order by position_number ASC")
Using JPA custom methods (as you used :
findByRackOrderByPositionNumberAsc
())
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);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
add a comment |
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)
add a comment |
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)
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)
edited Nov 16 '18 at 4:33
answered Nov 16 '18 at 4:25
darshakatdarshakat
391111
391111
add a comment |
add a comment |
JpaRepository interface extends PagingAndSortingRepository interface.
So, you can use endpoint ashttp://{base}/api/v1/rackPositions/findbyRack
or http://{base}/api/v1/rackPositions/find-byRack
by avoiding camel caps.
add a comment |
JpaRepository interface extends PagingAndSortingRepository interface.
So, you can use endpoint ashttp://{base}/api/v1/rackPositions/findbyRack
or http://{base}/api/v1/rackPositions/find-byRack
by avoiding camel caps.
add a comment |
JpaRepository interface extends PagingAndSortingRepository interface.
So, you can use endpoint ashttp://{base}/api/v1/rackPositions/findbyRack
or http://{base}/api/v1/rackPositions/find-byRack
by avoiding camel caps.
JpaRepository interface extends PagingAndSortingRepository interface.
So, you can use endpoint ashttp://{base}/api/v1/rackPositions/findbyRack
or http://{base}/api/v1/rackPositions/find-byRack
by avoiding camel caps.
edited Nov 16 '18 at 5:50
Kartik
4,29731537
4,29731537
answered Nov 16 '18 at 4:40
WGSSAMINTHAWGSSAMINTHA
10210
10210
add a comment |
add a comment |
There are 2 ways to sort result in Spring data JPA
Using Custom JPA Query as below
@Query("Select r from rack order by position_number ASC")
Using JPA custom methods (as you used :
findByRackOrderByPositionNumberAsc
())
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);
add a comment |
There are 2 ways to sort result in Spring data JPA
Using Custom JPA Query as below
@Query("Select r from rack order by position_number ASC")
Using JPA custom methods (as you used :
findByRackOrderByPositionNumberAsc
())
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);
add a comment |
There are 2 ways to sort result in Spring data JPA
Using Custom JPA Query as below
@Query("Select r from rack order by position_number ASC")
Using JPA custom methods (as you used :
findByRackOrderByPositionNumberAsc
())
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);
There are 2 ways to sort result in Spring data JPA
Using Custom JPA Query as below
@Query("Select r from rack order by position_number ASC")
Using JPA custom methods (as you used :
findByRackOrderByPositionNumberAsc
())
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);
answered Nov 16 '18 at 7:43
TheSprinterTheSprinter
573316
573316
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I have always used the
findBy...OrderBy..
interfaces - where have you seem that asort
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
tohttp://{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