7

RESTEasy (JAX-RS) Java サーバーを呼び出す Java クライアントがあります。一部のユーザーは、サーバーよりも新しいバージョンのクライアントを使用している可能性があります。

そのクライアントは、サーバーが認識していないクエリ パラメータを含むサーバー上のリソースを呼び出す可能性があります。サーバー側でこれを検出してエラーを返すことは可能ですか?

サーバーにまだ実装されていない URL をクライアントが呼び出すと、クライアントに 404 エラーが発生することは理解していますが、クライアントが実装されていないクエリ パラメータ (例: ?sort_by=last_name) を渡すとどうなりますか?

4

2 に答える 2

5

サーバー側でこれを検出してエラーを返すことは可能ですか?

はい、できます。最も簡単な方法はを使用することだと思います@Context UriInfo。メソッドを呼び出すことにより、すべてのクエリパラメータを取得できますgetQueryParameters()。したがって、不明なパラメータがあるかどうかがわかり、エラーを返すことができます。

しかし、クライアントが実装されていないクエリパラメータを渡した場合はどうなりますか

「不明な」パラメーターの処理に関する特別なサポートを実装しない場合、リソースが呼び出され、パラメーターは黙って無視されます。

個人的には、未知のパラメータは無視したほうがいいと思います。それらを単に無視する場合は、APIを下位互換性のあるものにすることが役立つ場合があります。

于 2011-10-03T22:14:25.177 に答える
0

You should definitely check out the JAX-RS filters (org.apache.cxf.jaxrs.ext.RequestHandler) to intercept, validate, manipulate request, e.g. for security or validatng query parameters.

If you declared all your parameters using annotations you can parse the web.xml file for the resource class names (see possible regex below) and use the full qualified class names to access the declared annotations for methods (like javax.ws.rs.GET) and method parameters (like javax.ws.rs.QueryParam) to scan all available web service resources - this way you don't have to manually add all resource classes to your filter. Store this information in static variables so you just have to parse this stuff the first time you hit your filter.

In your filter you can access the org.apache.cxf.message.Message for the incoming request. The query string is easy to access - if you also want to validate form parameters and multipart names, you have to reas the message content and write it back to the message (this gets a bit nasty since you have to deal with multipart boundaries etc).

To 'index' the resources I just take the HTTP method and append the path (which is then used as key to access the declared parameters.

You can use the ServletContext to read the web.xml file. For extracting the resource classes this regex might be helpful

String webxml = readInputStreamAsString(context.getResourceAsStream("WEB-INF/web.xml"));
Pattern serviceClassesPattern = Pattern.compile("<param-name>jaxrs.serviceClasses</param-name>.*?<param-value>(.*?)</param-value>", Pattern.DOTALL | Pattern.MULTILINE);
于 2012-10-31T01:19:25.533 に答える