1

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Evg
  • 141
  • 3
  • 13
  • Generally if you're trying to get several values, you'd use query parameters, not path variables. (This is a matter of HTTP semantics and not Spring MVC specifically.) – chrylis -cautiouslyoptimistic- May 05 '20 at 14:21
  • Does this answer your question? [How do I pass multiple parameter in URL?](https://stackoverflow.com/questions/10943635/how-do-i-pass-multiple-parameter-in-url) – Machavity May 05 '20 at 14:45

2 Answers2

1

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"

afarrapeira
  • 740
  • 8
  • 14
0
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 &

Dharman
  • 30,962
  • 25
  • 85
  • 135