Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of @RequestParam throws error in Spring 3

I am using the @RequestParam annotation to get the request parameters , and using the same to insert the values into the DB. I have setup the controller to re-direct to the same page which contains the text fields for the user to enter the values, which are being read using the @RequestParam annotation.

But after I enter the values into the text fields , and click submit , it throws this error

Request processing failed; nested exception is java.lang.IllegalArgumentException: Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.

I am a newbie to Spring 3 , and unable to understand the error. Can anybody please shed light on the same.

Thanks in advance, Vivek

like image 699
Vivek Avatar asked Sep 20 '11 11:09

Vivek


People also ask

What is the use of @RequestParam annotation in Spring?

In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.

What is the use of @RequestParam in Spring boot?

@RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.

Is @RequestParam required?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.

What is difference between @PathVariable and @RequestParam in Spring?

2) @RequestParam is more useful on a traditional web application where data is mostly passed in the query parameters while @PathVariable is more suitable for RESTful web services where URL contains values.


2 Answers

In order to inject the value of request parameter into your handler method parameter, either one of the following should be satisfied

  1. The name of the request parameter must match the name of the method parameter. e.g. Following will inject the request parameter named "studentName" into the method parameter studentName

    public String goToStep(@RequestParam String studentName)

  2. The request parameter name must be explicitly specified if it does not match the method parameter. The following will inject "nameOfStudent" request parameter into studentName:

    public String goToStep(@RequestParam("nameOfStudent") String studentName)

Please post your handler method code if your issue continues to persist.

like image 176
Babu Subburathinam Avatar answered Sep 24 '22 04:09

Babu Subburathinam


I asked for the version you are using because I ran with a similar problem a few days ago. I was using Spring 3.1.0.M2 and the same exception appeared when I was using @PathVariable in proxied @Controller.

It was caused by a resolved known bug. You just have to switch to 3.0.6 or try the nightly build 3.1.0.BUILD-SNAPSHOT. Of course, the latter option is not recommended for production environment.

like image 27
sinuhepop Avatar answered Sep 21 '22 04:09

sinuhepop