jersey
jersey copied to clipboard
IllegalArgumentException thrown from ParamConverter#fromString is converted into a default value by SingleValueExtractor
From ParamConverter#fromString
IllegalArgumentException - if the supplied string cannot be parsed or is null.
And from SingleValueExtractor#extract:
/**
* {@inheritDoc}
* <p/>
* This implementation extracts the value of the parameter applying the underlying
* {@link ParamConverter param converter} to the first value found in the list of potential multiple
* parameter values. Any other values in the multi-value list will be ignored.
*
* @param parameters map of parameters.
* @return extracted single parameter value.
*/
@Override
public T extract(final MultivaluedMap<String, String> parameters) {
final String value = parameters.getFirst(getName());
try {
return fromString((value == null && isDefaultValueRegistered()) ? getDefaultValueString() : value);
} catch (final WebApplicationException | ProcessingException ex) {
throw ex;
} catch (final IllegalArgumentException ex) {
return defaultValue();
} catch (final Exception ex) {
throw new ExtractorException(ex);
}
I'm trying to understand if this is a bug or expected behavior. I was expecting that this would get mapped to a 400 by default, but instead it silently gets converted to the default value.
Either way, I think both sets of JavaDoc should be enhanced to clarify this case as it seems to violate the principle of least surprise.
And you don't have a @DefaultValue annotation for the param, right? Assuming that's true, I agree that this is confusing. As it is...throwing an IllegalArgumentException has no affect, i.e., the incoming request passes right through to the resource class for normal processing
I can't recall anymore, but the code stands on it's own :)