Skip to content

optional boolean @RequestParam fails on missing parameter [SPR-4237] #8915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Dec 10, 2007 · 3 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 10, 2007

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:

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

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.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Kenny MacLeod commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Connor Barry commented

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:

ServletRequestUtils.getBooleanParameter(request, name, defaultVal)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants