0

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.

dernor00
  • 221
  • 1
  • 5
  • 17

3 Answers3

1

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.

kopaka
  • 535
  • 4
  • 17
0

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.

soumitra goswami
  • 818
  • 6
  • 29
  • I have an requirement to add the brackets and I don't have much experience to build urls like this – dernor00 Nov 13 '19 at 13:21
  • you can set the name of the parameter using the "name" attribute of the @RequestParam, but if you set the square brackets you will get an exception like - java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 – soumitra goswami Nov 13 '19 at 13:27
  • If you at all want to use square brackets this is what you can do- set the "name" attribute= "filter[date]" and then when making the api call url-encode the 3rd brackets like : ?filter%5Bdate%5D – soumitra goswami Nov 13 '19 at 13:31
0

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
}
N. berouain
  • 1,181
  • 13
  • 17