英文:
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 = "/ads/in/rubrics/{ids}")
public List<Ad> findAllAdInRubricByIds(@PathVariable("ids") List<Integer> 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&ids=1&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("/api/{version}/foo/{idFoo}")
public Void getFooNumber(@PathVariable("version") Integer version, @PathVariable("idFoo") Integer idFoo){
return "1";
}
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("/api/foo")
public Void getFooNumber(@RequestParam(value="version", required=false) Integer version, @RequestParam(value="idFoo", required=true) Integer idFoo){
return "1";
}
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("/ads/in/rubrics")
public Void findAllAdInRubricByIds(@RequestParam(value="ids", required=true) List<Integer> 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&ids2=2
For the first parameter use a ? and after that for each additional parameter a &
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论