简体   繁体   English

修改rest服务中GET的url路径

[英]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.我实现了一个简单的 rest GET 服务,我想修改该服务的 ulr。

The url now is: http://localhost:8011/types/id?date=2019-07-30T11:35:42 url 现在是: 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 ?像这样: http://localhost:8011/types/id filter[ date ] =2019-07-30T11:35:42过滤[日期] =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.这是我的 Get 服务,在值中我有“类型/ID”,但我不知道如何为请求的参数添加过滤器和括号。

    @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.我假设您使用 Spring 来定义您的 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此代码允许对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.但是, 在 URL 中不允许使用方括号,因此您可能需要重新考虑该特定方法。

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?为什么要在 RequestParam 中添加方括号?是因为您需要多种过滤器并想要区分它们吗? 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.此外,请确保这些值是 url 编码的,以避免字符串的任何部分使您的 url 无效。

Hope, this helps.希望这可以帮助。

Instead of using square brackets in an URL, I suggest you to pass a list of filters you want like this:我建议您不要在 URL 中使用方括号,而是传递您想要的过滤器列表,如下所示:

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

and change your controller like this:并像这样更改您的 controller:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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