Announcement Announcement Module
Collapse
No announcement yet.
Question on @RequestParam conversions Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Forgot to mention. I am using Spring 3.0.5

    Comment

    Working...
    X