Passing an Array or List to @Pathvariable - Spring/Java

JavaSpring MvcPath Variables

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

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";
}

Solution 3 - Java

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)

Solution 4 - Java

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

Solution 5 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDjokovicView Question on Stackoverflow
Solution 1 - JavaatamanromanView Answer on Stackoverflow
Solution 2 - JavaBruno Conrado SantosView Answer on Stackoverflow
Solution 3 - JavaJaimeView Answer on Stackoverflow
Solution 4 - JavadardoView Answer on Stackoverflow
Solution 5 - JavaAbdellah AbairView Answer on Stackoverflow