Amol Raje
Amol Raje

Reputation: 968

right way to retrieve query parameters in Spring Boot rest?

I am developing REST api using Spring Boot. I've a controller which accepts POST requests.

http://localhost:8085/carride/end-ride

In the above request i want to access the parameter ride_transection_id for finding particular transection object and also some other value as well. So basically i have 3 way to do that.

1. i can use @PathVariable

@RequestMapping(value = "/end-ride", method = RequestMethod.POST)
public ResponseEntity<?> endRide(@PathVariable("ride_transection_id") long ride_transection_id,@RequestBody 
SomeDTORequest someDTORequest ) {
//find transaction using path varibale 

}

2.i can use @RequestParam

    @RequestMapping(value = "/end-ride", method = RequestMethod.POST
    public @ResponseBody item getitem(@RequestParam("ride_transection_id") 
    long ride_transection_id,@RequestBody SomeDTORequest someDTORequest ){

    //find transaction using RequestParam varibale 

    }
  1. i can use DTO Object SomeDTORequest and accept ride_transection_id into that with other value as well.

    @RequestMapping(value = "/end-ride", method = RequestMethod.POST)
    public ResponseEntity<?> endRide(@RequestBody SomeDTORequest someDTORequest ) {
    //find transaction using path someDTORequest .getID()
    

    }

i am little bit confuses.just want ask which is safest and right way to access the ride_transection_id ?

thanks

Upvotes: 2

Views: 10799

Answers (2)

LHCHIN
LHCHIN

Reputation: 4009

Basically, all these 3 methods are fine. But if you want to develop or design RESTful services with best practices, I strongly recommend you should provide the querying service with @PathVariable and GET method such as GET /tickets/12. Otherwise, to digest request body with @RequestBody annotation to retrieve querying criteria for POST method is the second suggestion.

Because POST method is usually to be used for creating something. And for querying something, both @PathVariable and @RequestParam annotations are suitable for GET method. More specifically, @RequestParam is often to be used in Filtering, Sorting and Searching results. For example:

  • Filtering: GET /tickets?state=open - Here, state is a query parameter that implements a filter.
  • Sorting: GET /tickets?sort=-priority,created_at - Retrieves a list of tickets in descending order of priority. Within a specific priority, older tickets are ordered first.
  • Searching: GET /tickets?state=closed&sort=-updated_at - Retrieve recently closed tickets.

Please also refer to this article Best Practices for Designing a Pragmatic RESTful API. Hope this helps you! :)

Upvotes: 1

Amr Alaa
Amr Alaa

Reputation: 553

You can use any of them but every way is designed for a certain use.

Path variable: is used when you need to access an entity using a certain field for example i want to access an order and this order is defined by id so to access this order i need the following request Get /order/{id}

Request Parameter: when you want to send a specific variable or flag for a certain method for example Get /orders?is_shipped=true, so this will get all shipped orders or you may need orders at certain page Get /orders?page=1

Request body: when you need to update the entity by the put or patch request as you will update the entity using the entity's json representation which can be send through the request body for example PUT /orders/{id} body: {"title": "order_1"} then the order with id {id} will be updated with the new title

Spring data rest

See also

Upvotes: 2

Related Questions