spring-framework: Null passed in required `@RequestParam` now fails

Since 5.3.1 version null, passed for required nullable parameter throws Exception “is not present”

@RequestMapping("/findTree")
public void findTree(@RequestParam("parentId") Long parentId) {
  if (parentId == null) {
    //Root
  } else {
    //Is not root
  }
}

now, i send request "/myurl/findTree?parentId="

Expected behavior - parameter parentId passed to method as null Current behavior - exception within AbstractNamedValueMethodArgumentResolver

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (8 by maintainers)

Most upvoted comments

Is better to create some way to toggle this behavior.

Best regards

This is a consequence of #23939. We’ve tightened our rules for what required=true means in 5.3: It effectively means non-null for conversion results now. In a scenario like yours, an empty parameter String would still come in as such, but a converted value that led to a null fallback would be rejected as missing value.

Could you simply declare your parameter as required=false or as @Nullable?

Thanks for quick answer. It is ok for our small project - replace all parameters, that could be nullable, to Optional<?>. I’ve created this task because dont found any “migration guide” about this moment.

For larger projects this “feature” can be more paintful.

Best regards