1

In my application, there is a requirement of getting data based on some parameters. I just want to what is the better way to do.

The one way is, I can pass the list of parameters as a path variable.

The second way is, I can pass the request body, I think it is vague and I am not sure it is possible or not.

You can find the code below:

@GetMapping("/cities/{cityName}/latitude/{latitude}/longitude/{longitude}/cityId/{cityId}/street/{street}")
      public ResponseEntity<ResponseContainer<CityDto>> getCityByCityNameOrLatitudeAndLongitude() {
}

I just want to know how can I achieve the same.

There is one more question, E-commerce companies have big filter criteria so how they are achieving.

Vimit Dhawan
  • 343
  • 4
  • 15

4 Answers4

1

Although there is no hard & fast rule but I generally avoid sending a body in GET request because it's a bad design. You should also refer to this SO Post which contains discussion about using body in GET request. It's an opinionated post and there is no clear YES or NO, but you will get an idea.

HTTP GET with request body

You can either use Path params or query params depending on what those field represent.

Regarding the difference or which to use when I am quoting this answer, which mentions that although there is no hard rule but generally it's better to use params which can uniquely identify the resource as Path param (e.g. id, name etc) and if your param is supposed to do something like filtering/sorting e.g. records after Jan 1 2019 , then go for query param.

Also personally in one of my APIs (which performs filtering), I am using a generic query param, where I pass on JSON object in my query. So basically my API needs to search an object based on variable/multiple attributes. E.g. I have in my db , objects which have certain voltage, current, size etc. values. So, request might come with a combination of 1 or more. So to keep my API flexible, I have provided a query param which can accept JSON object.

So I make a request like this:

 {{SERVER}}/api/search?query={voltage:12V,size:10}

And in my API, I can convert this json object to corresponding POJO:

@GET    
@Path("/search")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response search(@QueryParam("query") String queryParam) throws Exception                        
{       
        Myobj obj = new Gson().fromJson(queryParam, Myobj.class);           
        // rest of code
tryingToLearn
  • 7,566
  • 8
  • 55
  • 84
0

By passing the parameters in the path, you are restricting yourself to extend your API. If you want to extend your API, for example, if you want to filter with criteria as Street1 (or) Street2 then your path wouldnot support it and it will force you to update your API. It is better to pass criteria objects in the body or url parameter. Amazon India is passing criteria like below. I have choosen mobiles with criteria as Manufacturer = Samsung or MI, Storage as 8gb or 4gb and they simply appended the criteria in the query parameters.

enter image description here

Praveen E
  • 706
  • 6
  • 11
0

There is a third way, Request Params.

@GetMapping
public ResponseEntity<ResponseContainer<CityDto>> getCityByCityNameOrLatitudeAndLongitude(@RequestParam("cityName") String cityName, @RequestParam("latitude") String latitude, @RequestParam("longitude") String longitude){
  // Your code
}

For more: 16.3.3.3 Binding request parameters to method parameters with @RequestParam

Parameters using this annotation are required by default, but you can specify that a parameter is optional by setting @RequestParam's required attribute to false (e.g., @RequestParam(value="id", required=false)).

https://docs.spring.io/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-ann-requestparam

Anant Goswami
  • 168
  • 1
  • 13
0

First of all, you are exposing a method to get cities. It's better approach to use path as /city/{somethingUnique}.

As you know there are duplicate city names around the world (like Paris, Melbourne).

Rather than having lots of methods(one method to query with id, other method to query with name, other method to query with lonlat, other method to query with countryid) to query your cities, I suggest you to have only one method. It makes your API more flexible. To achieve this, you can design with two different approaches:

  • Posting criterias as response body.
  • Passing criterias as a query string.
mahmutoflaz
  • 368
  • 3
  • 9