Results 1 to 2 of 2

Thread: Question on @RequestParam conversions

  1. #1
    Join Date
    Mar 2011
    Posts
    4

    Default Question on @RequestParam conversions

    I have some questions regarding RequestParam conversions.

    For the most part I have simple conversions working however when it comes to more complex objects things arent working the way they would seem.

    For example. How are lists/arrays handled when the type is any other type of object other than string?

    Code:
    @ResponseBody
    RequestMapping(value = "/foo")
    public String foo(@RequestParam(value ="bar", required=true) List<Integer> fooIds) {
    ...
    }
    http://localhost:8080/spring/foo?bar=1,2,3
    or
    http://localhost:8080/spring/foo?bar=1&bar=2&bar=3

    Whenever I try to access the list of fooIds as Integers I get a Cannot cast from String to Integer exceptions. I would have thought this simple conversion would be accomplished. If i change the type to List<String> everything is works as expected.

    Now for more complex objects:
    Code:
    public Foo {
      public Foo(List<String> bars) {
        .....
      }
    }
    Code:
    @ResponseBody
    RequestMapping(value = "/foo")
    public String foo(@RequestParam(value ="bar", required=true) Foo myFoo) {
    ...
    }
    Now in this example I would think that the Foo class would receive a List<String> as a constructor argument but it doesn't'. If i change or add a constructor that expects a single string then it will work. How would I handle this situation?

    Obviously I am missing something so can someone please clarify these issues for me?

    Thanks for any help!

  2. #2
    Join Date
    Mar 2011
    Posts
    4

    Default

    Forgot to mention. I am using Spring 3.0.5

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •