简体   繁体   中英

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.

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.

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM