By Djokovic


2012-03-08 18:58:16 8 Comments

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

5 comments

@Abdellah Abair 2020-05-10 03:37:14

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

@Bruno Conrado Santos 2014-10-30 11:51:39

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

@Idcmp 2015-09-18 00:53:10

Confirmed the above works with Spring 4.2, likely with most of Spring 4.x. This is also the most Java-like answer offered.

@Priyal85 2017-10-01 17:01:34

Does Jersey support this pattern?

@Sush19 2018-09-14 06:10:35

got 400 error code, this small change worked for me. public String test(@PathVariable("firstNameIds") List<Integer> firstNameIds) {

@Sachin HR 2019-03-05 07:39:54

I am sending data as ["first","second"].Can you tell how to accept it at controller using PathVariable?

@Ved Prakash 2019-04-10 07:46:00

@SachinHR : List<String>

@한국인 2014-10-27 04:34:50

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)

@Schenz 2019-04-15 16:48:34

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...

@atamanroman 2014-03-10 11:22:20

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)

@Priyal85 2017-10-01 17:02:07

Does jersey support this pattern?

@RockingDev 2018-04-09 07:00:21

Yes ,Jersey supports this but in different way using PathSegment

@袁文涛 2019-03-20 10:35:17

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"

@dardo 2012-03-08 21:54:45

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

Related Questions

Sponsored Content

26 Answered Questions

29 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1977956 View
  • 2291 Score
  • 29 Answer
  • Tags:   java arrays

87 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

35 Answered Questions

[SOLVED] Create ArrayList from array

58 Answered Questions

[SOLVED] How to create a memory leak in Java?

28 Answered Questions

[SOLVED] How do I declare and initialize an array in Java?

  • 2009-07-29 14:22:27
  • bestattendance
  • 4537656 View
  • 2073 Score
  • 28 Answer
  • Tags:   java arrays

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4087584 View
  • 3526 Score
  • 66 Answer
  • Tags:   java random integer

29 Answered Questions

24 Answered Questions

[SOLVED] How do I POST JSON data with cURL?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

Sponsored Content