4

When I create any repository in spring framework like following it gives method by default to get all records of the entity using this API

GET : http://localhost:3000/api/addresses

It sends data from ascending order But If I want my data in descending order than How can I specify this ?

Address Repository

    public interface AddressRepository extends JpaRepository<Address, Long> 

    {
    }
3
  • If it is findAll, then you can override this and add a condition for order by in your query Commented Jun 22, 2017 at 6:14
  • @CrazyMac My Query ? NO :| I didn't write any Query it is Spring default call to get records Commented Jun 22, 2017 at 6:16
  • What is your front end. Are you using Angular ? If yes, then you can use orderBy : 'name' as an approach to sort Commented Jun 22, 2017 at 7:06

3 Answers 3

7

Perhaps,you can :

localhost:3000/api/addresses?sort={property},desc 

this will help you sort by property desc

Sign up to request clarification or add additional context in comments.

1 Comment

it will work for you.localhost:3000/api/addresses?sort=id,desc that will sorted by the property of id desc
4

You can also specify this as part of your request, take a look at documentation here Sorting.

Also please take a look at similar answer here.

Comments

0

Try inside the AddressRepository class something like:

public List<Address> findAllByOrderByIdDesc();

of course you can change the "ById" part with any other field you want to use for sorting.

1 Comment

I know all these methods :\ I don't want to write any method just wants to modify GET : localhost:3000/api/addresses this query so that get records in desc order

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.