REST Web 服务使用 Restlet 接受 POST - 最佳实践
我有我的资源和他们典型的重写方法来处理 POST 请求。
public void acceptRepresentation(Representation rep) {
if (MediaType.APPLICATION_XML.equals(rep.getMediaType())) {
//Do stuff here
}
else {
//complain!
}
}
我想知道的是处理 XML 数据包的最佳实践。 我看到很多使用 Form 的示例 - 但肯定有一种方法可以使用 Representation 对象本身或将其转换为一些有用的 XML 对象???
非常感谢您对如何解析资源中传入的 XML 的任何帮助。
I have my resource and they typical overridden method to handle POST requests.
public void acceptRepresentation(Representation rep) {
if (MediaType.APPLICATION_XML.equals(rep.getMediaType())) {
//Do stuff here
}
else {
//complain!
}
}
What I want to know is the best practice to handle my packet of XML. I see a lot of examples using a Form - but surely there is a way to work with the Representation object itself or cast it to some useful XML object???
Any help on how you should and do parse incoming XML in your resource is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这更像是我一直在寻找的回应。 感谢 Thierry Boileau 的回答:
This is more of the kind of response I was looking for. Thanks to Thierry Boileau for the answer:
我们目前使用 RESTeasy 来完成此操作,它是一种替代的 JAX-RS 实现。 我们使用 JAXB 绑定(注释)在 XML 和模型 POJO 之间进行映射,并向 JAX-RS 指定一个 JAXB 提供程序,以便它知道如何进行。 我们的 RESTful 中对此进行了描述Java EE 中的 Web 服务与 RESTEasy (JAX-RS) 文章,可能会有所帮助。
更新:对于 Restlet,JAXB 扩展 可能就是您所需要的。
We currently do this using RESTeasy, which is an alternative JAX-RS implementation. We use JAXB bindings (annotations) to map between the XML and our model POJOs, and specify a JAXB provider to JAX-RS so it knows how. This is described in our RESTful web services in Java EE with RESTEasy (JAX-RS) article, which may help.
Update: for Restlet, the JAXB extension might be what you need.
通过
representation.getText()
方法,您可以获得一个可以输入 SAX 解析器或 dom 读取器的 String。Through the
representation.getText()
method, you can get a String that can be fed into a SAX parser or dom reader.@Simon E
我不明白:您使用的是哪种 Java REST 实现?
的示例
所以,我只是给你一个使用 JAX-RS (Jersey 实现) 服务器部分(某些 REST 类的方法)
: 客户端部分(某些 JUnit 的方法)测试):
就是这样!
如果您在导入时遇到问题:
服务器需要 javax.ws.rs.* 和 javax.ws.rs.core.*
客户端需要 com.sun.jersey.api.client.* 和
com.sun.jersey.api.representation.*
无论如何,我会给你建议使用 JAX-RS 而不是
替代实现,因为 JAX-RS 将成为
即将推出的Java EE 6
@Simon E
I don't understand: which REST implementation for Java are you using?
So, I just give you an example of using JAX-RS (Jersey implementation)
The server part (method of some REST class):
The client part (method of some JUnit test):
That's it !!!
In case you have trouble with imports:
Server needs javax.ws.rs.* and javax.ws.rs.core.*
Client needs com.sun.jersey.api.client.* and
com.sun.jersey.api.representation.*
In any way, I would give you the advice to use JAX-RS rather than
alternative implementations, because JAX-RS will be part of the
upcoming Java EE 6
即使在restlet 2.0中也是如此吗?
我使用restlet 2.0m6,这是我使用的代码片段 -
@Post
public Representation process(Representation entity)
{
try
{
DomRepresentation dom = new DomRepresentation(entity);
文档 d = dom.getDocument();
。
。
} catch(Exception e)
{ e.printStackTrace(); 它
在 dom.getDocument() 行抛出空指针异常。 这意味着没有数据实际到达。
我的弹性位看起来像这样 - var service : HTTPService = new HTTPService(); 服务.方法=“POST”; service.contentType="application/xml" service.url=url; var token :AsyncToken = service.send(params);
其中 params 是一个 XML 对象。
Is this the same procedure even in restlet 2.0??
I use restlet 2.0m6 and here is the code snippet that I use -
@Post
public Representation process(Representation entity)
{
try
{
DomRepresentation dom = new DomRepresentation(entity);
Document d = dom.getDocument();
.
.
} catch(Exception e)
{ e.printStackTrace(); }
and it throws a Null Pointer exception at the dom.getDocument() line. Which means no data actually arrived.
And my flex bit looks like this - var service : HTTPService = new HTTPService(); service.method="POST"; service.contentType="application/xml" service.url=url; var token :AsyncToken = service.send(params);
where params is an XML object.