2

I try to send a request in the Form:

list=Apple1&list=Apple2&list=Apple3 

whereas Apple is a complex object which is serialized in JSON:

 {"param1":"value1", "param2":"value2"}

I defined a Dispatcher Servlet in Spring to receive such requests, with a method like:

request(@RequestParam("list") POJO[] pojos){
//handle request
}

it is no problem to send a single POJO to such a method like

request(@RequestParam("single") POJO pojos){
//handle request
}

but as soon I try to transmit an Array Spring is dying with an Exception like:

java.lang.NoSuchMethodException: [Lmy.package.Apple;.<init>()

How can I teach the dispatcher servlet to deserialize arrays correctly? Thanks for you help

2
  • use a @RequestBody and pass the request body as json Commented Sep 18, 2012 at 16:26
  • no this is not working for me, since I am using methods with more then one parameter, so something like : request(@RequestParam POJO[] newArray, @RequestParam POJO[] oldArray) should be possible. Commented Sep 18, 2012 at 16:30

2 Answers 2

2

I had this error when I added the @RequestParam annotation to an interface my dispatcher servlet implements, not to the dispatcher servlet itself. In this case Spring would ignore the annotation and try to instantiate the array of apples directly, what would result in the error message you supplied: java.lang.NoSuchMethodException: [Lmy.package.Apple;.<init>().

Sign up to request clarification or add additional context in comments.

1 Comment

Amazing, I have a similar setup. I used an Interface where I defined the RequestParam Annotation, which is actually no problem for Spring, but when it comes to unmarshalling Spring somehow gets this wrong, and you need to provide the annotation additionally in the implementation, what a mess! Now it works!
1

The exception you get means under the hood your app expects Apple to have no-arg constructor and fails finding it (after Oracle's Troubleshooting page):

An IllegalArgumentException is thrown because the zero-argument constructor was requested and an attempt was made to pass an argument. The same exception would be thrown if the constructor was passed an argument of the wrong type.

$ java ConstructorTroubleAgain int
java.lang.NoSuchMethodException: ConstructorTroubleAgain.<init>(int)
        at java.lang.Class.getConstructor0(Class.java:2706)
        at java.lang.Class.getConstructor(Class.java:1657)
        at ConstructorTroubleAgain.main(ConstructorTroubleAgain.java:26)

which can mean... anything. Does Jackson deserializing from JSON string to Apple work well? How is your Apple object created? Is Apple1 properly escaped string? Also can you try with

list[]=Apple1&list[]=Apple2&list[]=Apple3

and

request(@RequestParam("list[]") Apple[] apples)

because last time I found Spring didn't handle arrays well if they don't have [] part.

2 Comments

Does Apple class have no-arg constructor and setters for all fields?
Yes, I forgot to mention that, but thanks I know that this can be an issue.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.