dernor00
dernor00

Reputation: 271

Modify the url path for GET in the rest service

I implemented a simple rest GET service and I want to modify the ulr for that Service.

The url now is: http://localhost:8011/types/id?date=2019-07-30T11:35:42

And I want to add a filter and to add in the date some brackets [ ],

like this: http://localhost:8011/types/id?filter[ date ]=2019-07-30T11:35:42

Here is my Get service, in the values i have the "types/id" but I don't know how to add filter and brackets for the requested params.

    @RequestMapping(value = "types/id", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<?> getTicketids( @RequestParam(required = false) String date)
    {
        ...
    }

I would appreciate any suggestion on what I could change or what I should read.

Upvotes: 0

Views: 284

Answers (3)

N. berouain
N. berouain

Reputation: 1311

Instead of using square brackets in an URL, I suggest you to pass a list of filters you want like this:

http://localhost:8011/types/id?filters=firstValue,secondValue,thirdValue

and change your controller like this:

@RequestMapping(value = "types/id", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public void receiveArrayOfValues(@RequestParam String[] filters) 
{
  // Handle values here
}

Upvotes: 0

kopaka
kopaka

Reputation: 792

I assume you are using Spring for defining your RestController. In general, parameters should work like this:

public ResponseEntity<?> getTicketids( @RequestParam(name = "filterDate", required = false) String date)
    {
        ...
    }

This code allows requests to http://localhost:8011/types/id?filterDate=mydate

However, square brackets are not allowed in an URL, so you might want to reconsider that specific approach.

Upvotes: 1

soumitra goswami
soumitra goswami

Reputation: 891

Why do you want to add square brackets to the RequestParam?is it because you are expecting mutiple kinds of filters and want to differentiate between them? in that case, you can instead add two request parameters-

?filterName=date&filterValue=2019-07-30T11:35:42

Also, make sure the values are url-encoded to avoid any part of the string making your url invalid.

Hope, this helps.

Upvotes: 0

Related Questions