I have a Java client that calls a RESTEasy (JAX-RS) Java server. It is possible that some of my users may have a newer version of the client than the server.
That client may call a resource on the server that contains query parameters that the server does not know about. Is it possible to detect this on the server side and return an error?
I understand that if the client calls a URL that has not been implemented yet on the server, the client will get a 404 error, but what happens if the client passes in a query parameter that is not implemented (e.g.: ?sort_by=last_name
)?
Can query params be optional? As query parameters are not a fixed part of a path, they can be optional and can have default values.
The JAX-RS @QueryParam annotation binds the value(s) of a HTTP query parameter to a resource method parameter, resource class field, or resource class bean property.
Query parameters are extracted from the request URI query parameters and are specified by using the javax. ws. rs. QueryParam annotation in the method parameter arguments.
Is it possible to detect this on the server side and return an error?
Yes, you can do it. I think the easiest way is to use @Context UriInfo
. You can obtain all query parameters by calling getQueryParameters()
method. So you know if there are any unknown parameters and you can return error.
but what happens if the client passes in a query parameter that is not implemented
If you implement no special support of handling "unknown" parameters, the resource will be called and the parameter will be silently ignored.
Personally I think that it's better to ignore the unknown parameters. If you just ignore them, it may help to make the API backward compatible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With