0

I have an endpoint that takes a request parameter: http://localhost/test?parameter=123 When somebody calls this endpoint with a string instead of an integer he gets a BAD_REQUEST response because the string can not be converted.

Is it possible to ignore conversion exceptions on a request parameter and just leave it empty?`

Currently my code looks like this:

@RequestMapping(value = "/test") public void doSomething(@RequestParam(required = false) Integer parameter) {...} 
1
  • Why not taking the parameter simply as String and convert yourself in the method code? Commented Oct 23, 2013 at 10:44

1 Answer 1

1

You should take the parameter as String and convert yourself.

By saying it should be Integer in your method signature you require this indeed to be an integer. If it was not it is indeed BAD_REQUEST. If you want other custom scenario you should implement it yourself.

@RequestMapping(value = "/test") public void doSomething(@RequestParam(required = false) String parameter) { Integer parameterValue = null; if (parameter != null) { try { parameterValue = Integer.valueOf(parameter); } catch (NumberFormatException ex) { // No-op as already null } } // At this point the parameterValue is either null if not specified or specified as non-int, or has the integer value in it } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.