0

The following client code

FormData formData = FormData(document.getElementById(formElemId));
formData.append('version', null);

when received in Server Side via Spring MVC as below

@RequestParam(value = 'version', required = false) Integer versionNumber

throws the 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"

Mohan Krishnan
  • 353
  • 3
  • 16

1 Answers1

0

You can resolve this issue by adding defaultValue like below.

@RequestParam(value = "version", required = false, defaultValue = "0") Integer versionNumber

see documentation

Alien
  • 15,141
  • 6
  • 37
  • 57
  • Unfortunately I cannot set a default value to the version (Application Logic). I'll have to handle the null in client itself. So I was wondering what turns my null object in client to string null when received in server. – Mohan Krishnan Nov 08 '18 at 13:58
  • actually it was a bug in previous versions which has been resolved now...try with latest versions defaultValue is not needed. – Alien Nov 08 '18 at 14:01
  • Oh thanks for that. Can you please let me know on which version is this bug was fixed. Am currently on 4.3.9 – Mohan Krishnan Nov 08 '18 at 14:08
  • 1
    This was considered a bug in 2013: https://jira.spring.io/browse/SPR-10180 and was fixed with version 3.2.2. Problem shouldn't occur in any versions after that and your code should work just fine. – Alien Nov 08 '18 at 14:18
  • With the latest spring boot, if I use `@ModelAttribute`, this exception still exist – frank Oct 21 '19 at 02:55