编写 QBE Restful 方法的最佳方法?
我正在使用 RESTeasy 和 Seam 开发一些 Web 服务。我想创建的服务之一是“按示例查询”服务。
我首先尝试像这样编写代码:
@GET
@Produces("application/xml")
@Consumes("application/xml")
@Path("/matching")
public MessageList getMatchingMessages(Message msg);
不幸的是,这在客户端中产生了复杂性:
[testng] [Parser] Running:
[testng] C:\Users\bdw\workspace-shepherd\GPRI\test-build\testng-Test.xml
[testng] java.lang.RuntimeException: java.lang.ClassCastException: org.apache.commons.httpclient.methods.GetMethod cannot be cast to org.apache.commons.httpclient.methods.EntityEnclosingMethod
[testng] at org.jboss.resteasy.client.core.ClientInvoker.invoke(ClientInvoker.java:104)
[testng] at org.jboss.resteasy.client.core.ClientProxy.invoke(ClientProxy.java:59)
[testng] at $Proxy138.getMatchingMessages(Unknown Source)
等等。此方法的一个变体允许指定日期范围,但显然,这种形式也不起作用:
@GET
@Produces("application/xml")
@Consumes("application/xml")
@Path("/matching")
public MessageList getMatchingMessages(@QueryParam("startDate") Date start,
@QueryParam("endDate") Date end, Message msg);
当我开始考虑时,将 Message 传递给 GET 方法可能会违反 REST 的精神。当然,从方法中删除 Consumes 注释可以让客户端运行时不会出现这些错误。但它引出了一个问题:编写采用非基于 URL 的 XML 输入并生成 XML 输出的查询方法的正确方法是什么?或者同时采用 XML 和基于 url 的参数并生成 XML 输出?
I'm working on developing some web services using RESTeasy and Seam. One of the services I'd like to create is a Query by Example service.
I first tried to code it like this:
@GET
@Produces("application/xml")
@Consumes("application/xml")
@Path("/matching")
public MessageList getMatchingMessages(Message msg);
This, unfortunately, produced complications in the client:
[testng] [Parser] Running:
[testng] C:\Users\bdw\workspace-shepherd\GPRI\test-build\testng-Test.xml
[testng] java.lang.RuntimeException: java.lang.ClassCastException: org.apache.commons.httpclient.methods.GetMethod cannot be cast to org.apache.commons.httpclient.methods.EntityEnclosingMethod
[testng] at org.jboss.resteasy.client.core.ClientInvoker.invoke(ClientInvoker.java:104)
[testng] at org.jboss.resteasy.client.core.ClientProxy.invoke(ClientProxy.java:59)
[testng] at $Proxy138.getMatchingMessages(Unknown Source)
and so on. A variation of this method allows for a date range but, obviously, doesn't work in this form either:
@GET
@Produces("application/xml")
@Consumes("application/xml")
@Path("/matching")
public MessageList getMatchingMessages(@QueryParam("startDate") Date start,
@QueryParam("endDate") Date end, Message msg);
When I got to thinking about it, passing a Message to a GET method might violate the spirit of REST. Certainly, removing the Consumes annotation from the method allows the client to run without these errors. But it begs the question, what is the right way to write a query method that takes non-URL-based, XML input and produces XML output? Or that takes both XML and url-based parameters and produces XML output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您认为您可能想要缓存结果吗?如果答案是否定的,那么您可以简单地使用 POST 来代替。
如果答案是您可能想要缓存结果,那么您可以这样做:
说了这么多,您确定不能只使用查询参数而不是传递 XML 吗?
Do you think you may want to cache the results? If the answer is no, then you can simply use POST instead.
If the answer is maybe you might want to cache the results then you could do:
Having said all of this, are you sure you can't just use query parameters instead of passing XML?
我无法帮助您解决问题的编码方面,但如果您传递非 URL 输入,那么 POST 方法将比 GET 更合适。您实际上正在创建一个新资源:与示例匹配的消息列表。
I can't help with the coding side of your question, but if you're passing in a non-URL input, then the POST method would be more appropriate than GET. You are in effect creating a new resource: a list of messages that match the example.