82

I am doing a simple 'get' in JBoss/Spring. I want the client to pass me an array of integers in the url. How do I set that up on the server? And show should the client send the message?

This is what I have right now.

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable List<Integer> firstNameIds)
{
     //What do I do??
     return "Dummy"; 
}

On the client I would like to pass something like

http://localhost:8080/public/test/[1,3,4,50]

When I did that I get an error:

java.lang.IllegalStateException: Could not find @PathVariable [firstNameIds] in @RequestMapping

1

5 Answers 5

91
GET http://localhost:8080/public/test/1,2,3,4

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable String[] firstNameIds)
{
    // firstNameIds: [1,2,3,4]
    return "Dummy"; 
}

(tested with Spring MVC 4.0.1)

6
  • Does jersey support this pattern?
    – Priyal
    Oct 1, 2017 at 17:02
  • Yes ,Jersey supports this but in different way using PathSegment
    – MiniSu
    Apr 9, 2018 at 7:00
  • Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "20056,20056"
    – 袁文涛
    Mar 20, 2019 at 10:35
  • But how to distinguish between /{id} and /{ids}? Tried to do it like @GetMapping(value=["""/{id:\d+}"""]) and @GetMapping("""/{ids:\d+[,\d+]+}""") but now Spring tells me that my-url/123 is ambigous (which isn't the case)...
    – spyro
    Feb 1, 2021 at 14:52
  • @spyro I guess you could check if ids.size() == 1? Feb 11, 2021 at 13:57
50

You should do something like this:

Call:

GET http://localhost:8080/public/test/1,2,3,4

Your controller:

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable List<Integer> firstNameIds) {
     //Example: pring your params
     for(Integer param : firstNameIds) {
        System.out.println("id: " + param);
     }
     return "Dummy";
}
5
  • Confirmed the above works with Spring 4.2, likely with most of Spring 4.x. This is also the most Java-like answer offered.
    – Idcmp
    Sep 18, 2015 at 0:53
  • Does Jersey support this pattern?
    – Priyal
    Oct 1, 2017 at 17:01
  • got 400 error code, this small change worked for me. public String test(@PathVariable("firstNameIds") List<Integer> firstNameIds) {
    – Sushil
    Sep 14, 2018 at 6:10
  • 1
    I am sending data as ["first","second"].Can you tell how to accept it at controller using PathVariable?
    – Sachin HR
    Mar 5, 2019 at 7:39
  • @SachinHR : List<String>
    – Onic Team
    Apr 10, 2019 at 7:46
12

if you want to use Square brackets - []

DELETE http://localhost:8080/public/test/[1,2,3,4]

@RequestMapping(value="/test/[{firstNameIds}]", method=RequestMethod.DELETE)
@ResponseBody
public String test(@PathVariable String[] firstNameIds)
{
    // firstNameIds: [1,2,3,4]
    return "Dummy"; 
}

(Tested with Spring MVC 4.1.1)

1
  • 1
    This works great when wrapping in square brackets, but what if I'm required to send a list wrapped in curly braces? that always seems to blow-up on me...
    – Schenz
    Apr 15, 2019 at 16:48
3

Could do @PathVariable String ids, then parse the string.

So something like:

@RequestMapping(value="/test/{firstNameIds}", method=RequestMethod.GET)
@ResponseBody
public String test(@PathVariable String firstNameIds)
{
     String[] ids = firstNameIds.split(",");
     return "Dummy"; 
}

You'd pass in:

http://localhost:8080/public/test/1,3,4,50
0

At first, put this in your code (Add @PathVariable) :

@GetMapping(path="/test/{firstNameIds}",produces = {"application/json"})
public String test(@PathVariable List<String> firstNameIds)
{
     return "Dummy"; 
}

You'd pass in: http://localhost:8080/public/test/Agent,xxx,yyyy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.