JAX-RS和未知查询参数 [英] JAX-RS and unknown query parameters

查看:81
本文介绍了JAX-RS和未知查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java客户端,该客户端调用RESTEasy(JAX-RS)Java服务器.我的某些用户可能具有比服务器新的客户端版本.

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?

我了解到,如果客户端调用服务器上尚未实现的URL,则客户端将收到404错误,但是如果客户端传入未实现的查询参数(例如:)?

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

推荐答案

是否可以在服务器端检测到此错误并返回错误?

Is it possible to detect this on the server side and return an error?

是的,您可以做到.我认为最简单的方法是使用@Context UriInfo.您可以通过调用getQueryParameters()方法获取所有查询参数.因此,您知道是否有任何未知参数,并且可以返回错误.

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.

我个人认为最好忽略未知参数.如果您只是忽略它们,则可能有助于使API向后兼容.

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.

这篇关于JAX-RS和未知查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆