-
Notifications
You must be signed in to change notification settings - Fork 358
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
Having @QueryParam without @DefaultValue throws expensive IllegalArgumentException for every missing querystring parameter #5260
Comments
- Instead of throwing IllegalArgumentException when value is null, just return null. - Throwing exceptions is expensive, especially when it's done for every querystring parameter that has no `@DefaultValue` (and is not present in the request querystring)
In general, this is a very good point. I do not know why Jersey was implemented that way - I try to dig deeper to see if that's necessary. We would love to have Jersey as fast possible - and replacing these exception-catching mechanisms can improve the performance. However, we need to check if the IAE can in any case be thrown. There might be a requirement from the Javadoc or somewhere to see the IAE. I hope not, though. |
From what i can find in the code, Line 65 in 7b6d2f8
The latter was already checked when calling the fromString method, so it seems superfluous to use an exception for it. The question is if IAE can also be thrown in other (valid) cases, but it would still be viable to keep the catch clause for IAE in for those situations. But just don't throw it when a null value is passed. |
The Javadoc to ParamConverter mandates an IllegalArgumentException is thrown if the supplied string cannot be parsed or is null. In Jersey, the IAE is always converted to null. But in theory, any user can grab all That's what likely never will happen, though. But a property should enable requesting spec-compliant behavior. |
Thanks for fixing this, will try it out and run our performance tests against 2.40. |
@jansupol i verified that the changes in 2.40 indeed work as expected. The @DefaultValue annotations are no longer needed and yield the same performance metrics now. Thanks! |
When performance testing our new Jersey 2.38 application, i noticed that some endpoints were much slower than before (when using Apache Wink).
I narrowed it down with Yourkit and noticed that
IllegalArgumentException
is thrown for every querystring parameter that's not present in the request querystring. When adding a@DefaultValue
, this doesn't occur though.See relevant code here: https://github.com/eclipse-ee4j/jersey/blob/2.38/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java#L63
Throwing exceptions to catch "regular" application flow is generally a bad habit and decreases performance of the application (https://www.baeldung.com/java-exceptions-performance).
Is this intended behavior? E.g. when specifying a
@QueryParam
without@DefaultValue
, does that automatically make it a required querystring parameter?The JAX-RS specification doesn't seem to reflect this, having an optional querystring parameter without a default value seems legit and should just return null.
It becomes more noticable when you have endpoints with a lot of optional querystring parameters.
More info can be found here https://stackoverflow.com/a/35625547/3032647.
I would suggest that the relevant method could also just return null in this case:
The same goes for querystring parameters of different types, like int. These throw a NumberFormatException.
The text was updated successfully, but these errors were encountered: