Usage of #RequestParam throws error in Spring 3 - spring

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

In order to inject the value of request parameter into your handler method parameter, either one of the following should be satisfied
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)
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.

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.

Related

Mapping of missing URI variables to Request Mapping

I've developed a Spring API /getFileData, which accepts three URI parameters viz. businessDate/fileName/recordId. It is possible to have any of them can be passed as null. But I still want my API to be working in this case also. How can I achieve this?
I've tried using #GetMapping("getFileData/{businessDate}/{fileName}/{recordId}", "getFileData/{businessDate}//", "getFileData/{businessDate}/{fileName}/")..so on like this for all possible combinations.
#RequestMapping(value = "/getFileData/{businessDate}/{fileName}/{recordId}", method = RequestMethod.GET)
I want this API to be working for all the combination of URI parameters if something get missed out. for example someone requested,
/getFileData///22 or
/getFileData/22Dec2018/ or
/getFileData//treasure/22
You can do that with a #RequestParam of type java.util.Map.
With your design, you will have various #PathVariable params in the controller method as well as the order of path variables /{var1}/{var2}... constructs the url so I don't think it would be possible to skip a path variable in the url and still call the same controller method.

Spring Data Mongo: IllegalArgumentException trying to use field reference for minus()

I'm trying to use the $subtract operator to project the difference between two fields as a separate third field using this line of code:
pipeline.add(Aggregation.project("createdTime","modResult").andExpression("createdTime").minus("modResult").as("bucketedTime"));
However, when I try to perform the aggregation, I get the following exception:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: can't serialize class org.springframework.data.mongodb.core.aggregation.Fields$AggregationField
What am I doing wrong here? I've noticed that if I provide an integer instead of a field name, there is no issue. Thanks for your help.
As per my question here, it turns out I should have been using and() instead of andExpression(). The latter tries to interpret the argument as an expression, such as field1 + field2 as a more convenient way of doing .and("field1").plus("field2").

Url in a path variable spring restful service

When I am passing email address as path variable it is throwing following error
Console --> 2015-02-09 16:30:06,634 WARN - GET request for "http://localhost:8181/abc/users/testabghtmail#gmail.com" resulted in 406 (Not Acceptable); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 406 Not Acceptable
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:607)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:565)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:521)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:439)
at RestClient.main(RestClient.java:35)
I have tried lots of cases, so I finally found the problem with last domain like .com and .org which are internationalize domains. So instead of "testabghtmail#gmail.com" if I pass "testabghtmail#gmail.dom" it will work perfectly fine.
My code is
#RequestMapping(value = "users/{emailId:.*}", method = RequestMethod.GET)
public Object searchUser(#PathVariable("emailId") String emailId){
logger.info("Inside search user --> emailId " + emailId);
return userService.findUserByuserId(emailId);
}
I found no answer to this. I think it's an http rule we can't have domains at last in prameters and can make a request.
So work around to this is just pass a slash at the end of the url and there you go.
Like modify "http://localhost:8181/abc/users/testabghtmail#gmail.com/" with "http://localhost:8181/abc/users/testabghtmail#gmail.com". And thanks to spring rest architecture, it will automatically omit the last slash and you will get "testabghtmail#gmail.com" as a parameter value.
Let me know if you guys find something else.

Spring MVC - HTTP status code 400 (Bad Request) for missing field which is defined as being not required

I have Spring MVC application with this controller method.
#RequestMapping(value = "/add", method = RequestMethod.POST)
public String addNumber(#RequestParam(value="number", required=false) Long number) {
...
return "redirect:/showAll/";
}
In my JSP I have a standard HTML form which is posting a value named "number" to the controller method above. However, if I leave out the value (do not enter anything into the text field) and POST the data to the controller, before the controller method is called my browser shows
HTTP Status 400 - Required Long parameter 'number' is not present
although the controller method annotation clearly defines the "number"-parameter as not required.
Does anyone have a slight idea of what could be going on?
Thank you.
PS: The exception that is being thrown is as follows:
org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'number' is not present
EDIT: This is a Spring 3.2.3.RELEASE bug ( see here). With version 3.1.4.RELEASE I do not have this problem anymore.
I came across the same situation, and this happens when your parameter is present in the request with an empty value.
That is, if your POST body contains "number=" (with empty value), then Spring throws this exception. However, if the parameter is not present at all in the request, it should work without any errors.
My problem was that some of the headers in a request I was sending with Postman were not present (were unchecked):
When I checked back the Content-Length header, the request worked fine (200 OK response).

Named Parameters rather than positional for Spring MessageSource.getMessage()

we are using Spring MessageSource to build error messages in our app.
We populate our error messages like this
dobInvalid = The DOB supplied {0} is invalid
We want to use named parameters so we can do
dobInvalid = The DOB supplied {dob} is invalid
Looking in the Api docs for getMessage it appears to suggest you can do this
http://static.springsource.org/spring/docs/1.2.x/api/org/springframework/context/MessageSource.html
args - Array of arguments that will be filled in for params within the
message (params look like "{0}", "{1,date}", "{2,time}" within a
message), or null if none.
Obviously we can write our own but was wondering if spring can do it and if anyone can provide an example or using named parameters rather positional parameters.
Cheers
Mark
AIUI, Spring MessageSource works with JDK MessageFormat, so there is no such a named parameter. {1,date} is an example, where "date" refers to formatType, no to an arbitrary named parameter.
The general form of a parameter is:
{ ArgumentIndex , FormatType , FormatStyle }

Resources