Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAX-RS and unknown query parameters

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)?

like image 297
Ralph Avatar asked Oct 03 '11 13:10

Ralph


People also ask

Is QueryParam optional?

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.

How would you describe the JAX RS @QueryParam annotation?

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.

Which of the following method is used to extract parameters from request?

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.


1 Answers

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.

like image 163
Tarlog Avatar answered Sep 28 '22 16:09

Tarlog