Reputation: 313
I have a rest API where I need to send page num as query parameter. When I send null, it gives me a bad request. Below is the rest API code
@RequestMapping(value = "/sample", method = RequestMethod.GET)
@ResponseBody
public String sample(
@RequestParam(value = "page-number", required = false, defaultValue = "1") final Integer pageNumber,
@RequestParam(value = "page-size", required = false, defaultValue = "50") final Integer pageSize) {
return "hello";
}
I am hitting the API with the following URL http://localhost:8000/sample?pageNumber=null
I am getting the below exception
"Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"null\"",
How do I handle null case?
Upvotes: 0
Views: 5317
Reputation: 611
While hitting any HTTP request, if you don't want to send any value of any request parameter, then please don't include that specific parameter in URL instead of sending null value to that parameter.
For e.g.
If you don't want to send any value in pageNumber request parameter, then please don't include pageNumber in request parameter.
So your request URL will be: http://localhost:8000/sample
If you will hit URL something like http://localhost:8000/sample?pageNumber=null
, then it will map "null" string literal to pageNumber request param, and you will get following exception:
"Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: \"null\"",
because you are expecting an Integer value that should be mapped with pageNumber request parameter not a string literal.
Upvotes: 2