You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I have a boolean @RequestParam(required=false)-annotated parameter on a @RequestMapping-annotated method, and a request comes in where that parameter is not specified, then I get an exception:
java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [boolean]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:237)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:47)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:41)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ArgumentsResolver.resolveArguments(AnnotationMethodHandlerAdapter.java:549)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:242)
Shouldn't this convert the missing parameter to false?
The intent is to declare such parameters as Boolean, allowing for null values - analogous to how we handle this in ServletRequestUtils. However, we should be throwing a nice exception in such a case: Primitive parameter types cannot be marked as required=false, essentially.
Alternatively, perhaps @RequestParam could take an optional "defaultValue" attribute? It'd have to be a string, subject to the same conversion rules as if the parameter had been present. This would work for primitives as well as reference types.
I agree, a defaultValue attribute would be really helpful here, to mimic the set of functions in ServletRequestUtils that allow a default to be set if it's not present, for example:
Kenny MacLeod opened SPR-4237 and commented
If I have a boolean
@RequestParam
(required=false)-annotated parameter on a@RequestMapping-annotated
method, and a request comes in where that parameter is not specified, then I get an exception:java.lang.IllegalArgumentException: Cannot convert value of type [null] to required type [boolean]: no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:237)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:47)
at org.springframework.beans.SimpleTypeConverter.convertIfNecessary(SimpleTypeConverter.java:41)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ArgumentsResolver.resolveArguments(AnnotationMethodHandlerAdapter.java:549)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:242)
Shouldn't this convert the missing parameter to false?
Affects: 2.5 final
Issue Links:
@RequestParam
The text was updated successfully, but these errors were encountered: