如何在GET请求中传递多个参数

huangapple go评论71阅读模式
英文:

how to pass several parameters in get request

问题

我正在尝试通过API检查我的方法是否有效

@GetMapping(value = "/ads/in/rubrics/{ids}")
public List<Ad> findAllAdInRubricByIds(@PathVariable("ids") List<Integer> ids) {
    return adService.findAllAdInRubricByIds(ids);
}
我该如何在GET请求中设置一些参数
这是我尝试的方式

http://localhost:9999/mvc/ad/ads/in/rubrics/ids&ids=1&ids=2
http://localhost:9999/mvc/ad/ads/in/rubrics/ids&ids1=1&ids2=2

始终收到400错误 无效的请求
英文:

I'm trying to check if my method works through the API

   @GetMapping(value = &quot;/ads/in/rubrics/{ids}&quot;)
        public List&lt;Ad&gt; findAllAdInRubricByIds(@PathVariable(&quot;ids&quot;) List&lt;Integer&gt; ids) {
            return adService.findAllAdInRubricByIds(ids);
        }

how can i set some parameters in get request?
that's how i tried

http://localhost:9999/mvc/ad/ads/in/rubrics/ids&amp;ids=1&amp;ids=2
http://localhost:9999/mvc/ad/ads/in/rubrics/ids&ids1=1&ids2=2

always get error 400 Bad Request

答案1

得分: 1

你将"PathVariables"与"RequestParams"混淆了。

"PathVariables"是请求路径中的变量。它不需要是路径的最后一个字符。

@GetMapping("/api/{version}/foo/{idFoo}")
public Void getFooNumber(@PathVariable("version") Integer version, @PathVariable("idFoo") Integer idFoo){
    return "1";
}

由于"PathVariables"是路径的一部分,它们总是必需的。如果您在请求中不包括它们,您将会调用另一个端点,或者如果请求无法映射到任何端点,将会得到404错误。

"RequestParams"是在请求URL末尾,在"?"字符之后接收到的参数。

@GetMapping("/api/foo")
public Void getFooNumber(@RequestParam(value="version", required=false) Integer version, @RequestParam(value="idFoo", required=true) Integer idFoo){
    return "1";
}

使用"RequestParams",您可以为每个参数定义是否需要。

您还可以混合使用它们,在同一个方法中使用"PathVariables"和"RequestParams"。

在第一个示例中,请求URL将是".../api/1/foo/25",而在第二个示例中,它将是".../api/foo?version=1&idFoo=25"。

至于使用数组或列表,如果您将参数定义为List,您可以发送多个具有相同名称的参数。

@GetMapping("/ads/in/rubrics")
public Void findAllAdInRubricByIds(@RequestParam(value="ids", required=true) List<Integer> ids){
    return adService.findAllAdInRubricByIds(ids);
}

在这种情况下,您可以使用".../ads/in/rubrics?ids=1&ids=2&ids=3&ids=4"。

英文:

You're confusing PathVariables with RequestParams.

A PathVariable is a variable in the request path. It doesn't need to be the last character.

@GetMapping(&quot;/api/{version}/foo/{idFoo}&quot;)
public Void getFooNumber(@PathVariable(&quot;version&quot;) Integer version, @PathVariable(&quot;idFoo&quot;) Integer idFoo){
    return &quot;1&quot;;
}

Since PathVariables are part of the path, they're always required. If you don't incluide them in the request you'll end up invoking another endpoint or getting a 404 if the request can't be mapped to any endpoint.

The RequestParams are the parameters received at the end of the request URL, after the "?" character.

@GetMapping(&quot;/api/foo&quot;)
public Void getFooNumber(@RequestParam(value=&quot;version&quot;, required=false) Integer version, @RequestParam(value=&quot;idFoo&quot;, required=true) Integer idFoo){
    return &quot;1&quot;;
}

With RequestParams you can define for each one of them if it's required or not.

You can also mix them and have in the same method PathVariables and RequestParams.

In the first example the request URL would be ".../api/1/foo/25", while in the second example it would be ".../api/foo?version=1&idFoo=25"

As for having an array or a list, if you define the parameter as a List you can send multiple parameters of the same name:

@GetMapping(&quot;/ads/in/rubrics&quot;)
public Void findAllAdInRubricByIds(@RequestParam(value=&quot;ids&quot;, required=true) List&lt;Integer&gt; ids){
    return adService.findAllAdInRubricByIds(ids);
}

In this case, you can use ".../ads/in/rubrics?ids=1&ids=2&ids=3&ids=4"

答案2

得分: 0

http://localhost:9999/mvc/ad/ads/in/rubrics/?ids1=1&ids2=2

对于第一个参数,请使用 ?,然后对于每个额外的参数,请使用 &。

英文:
http://localhost:9999/mvc/ad/ads/in/rubrics/?ids1=1&amp;ids2=2

For the first parameter use a ? and after that for each additional parameter a &

huangapple
  • 本文由 发表于 2020年5月5日 21:30:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61614380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定