I have my resource and they typical overridden method to handle POST requests.
我有我的资源和他们典型的重写方法来处理POST请求。
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???
我想知道的是处理我的XML数据包的最佳实践。我看到很多使用Form的例子 - 但肯定有一种方法可以使用Representation对象本身或将其转换为一些有用的XML对象???
Any help on how you should and do parse incoming XML in your resource is much appreciated.
任何有关如何应对和解析资源中的传入XML的帮助都非常感谢。
5 个解决方案
#1
This is more of the kind of response I was looking for. Thanks to Thierry Boileau for the answer:
这更像是我一直在寻找的那种回应。感谢Thierry Boileau的回答:
You can use two kinds of "XML representations": DomRepresentation and SaxRepresentation. You can instantiate both of them with the posted representation. E.g.: DomRepresentation xmlRep = new DomRepresentation(rep);
您可以使用两种“XML表示”:DomRepresentation和SaxRepresentation。您可以使用已发布的表示来实例化它们。例如:DomRepresentation xmlRep = new DomRepresentation(rep);
The DomRepresentation gives you access to the Dom document. The SaxRepresentation allows you to parse the XML doc with your own contentHandler. See the javadocs here 1 and here 2.
DomRepresentation允许您访问Dom文档。 SaxRepresentation允许您使用自己的contentHandler解析XML文档。请参阅此处的javadoc 1和此处2。
#2
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.
我们目前使用RESTeasy执行此操作,RESTeasy是另一种JAX-RS实现。我们使用JAXB绑定(注释)在XML和我们的模型POJO之间进行映射,并为JAX-RS指定JAXB提供程序,以便它知道如何。我们在Java EE中使用RESTEasy(JAX-RS)文章的RESTful Web服务中描述了这一点,这可能有所帮助。
Update: for Restlet, the JAXB extension might be what you need.
更新:对于Restlet,JAXB扩展可能就是您所需要的。
#3
Through the representation.getText()
method, you can get a String that can be fed into a SAX parser or dom reader.
通过representation.getText()方法,您可以获得可以提供给SAX解析器或dom读取器的String。
#4
@Simon E
I don't understand: which REST implementation for Java are you using?
我不明白:您使用的是哪种REST实现?
So, I just give you an example of using JAX-RS (Jersey implementation)
所以,我给你一个使用JAX-RS(Jersey实现)的例子
The server part (method of some REST class):
服务器部分(某些REST类的方法):
@POST
@Path("/upload")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public static Response upload(
@FormParam("name") String name,
@FormParam("content") String content)
throws Exception {
// NOTE: you get your content as String
// (do something here)
return Response.ok().build();
}
The client part (method of some JUnit test):
客户端部分(一些JUnit测试的方法):
@Test
public void uploadFile()
throws Exception {
String address = "http://0.0.0.0:8000/r/upload";
WebResource r = Client.create().resource(address);
Form form = new Form();
form.add("name", "test");
form.add("content", "SOME CONTENT GOES HERE");
String s = r.post(String.class, form);
System.out.println(s);
}
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.*
如果您在导入时遇到问题:服务器需要javax.ws.rs。*和javax.ws.rs.core。*客户端需要com.sun.jersey.api.client。*和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
无论如何,我会给你建议使用JAX-RS而不是替代实现,因为JAX-RS将成为即将到来的Java EE 6的一部分
#5
Is this the same procedure even in restlet 2.0??
即使在restlet 2.0中这是相同的程序吗?
I use restlet 2.0m6 and here is the code snippet that I use -
我使用restlet 2.0m6,这是我使用的代码片段 -
@Post
public Representation process(Representation entity)
公共代理过程(代表实体)
{
try
{
DomRepresentation dom = new DomRepresentation(entity);
DomRepresentation dom = new DomRepresentation(entity);
Document d = dom.getDocument();
文件d = dom.getDocument();
.
.
} catch(Exception e)
} catch(例外e)
{ e.printStackTrace(); }
{e.printStackTrace(); }
and it throws a Null Pointer exception at the dom.getDocument() line. Which means no data actually arrived.
它会在dom.getDocument()行抛出一个空指针异常。这意味着没有数据实际到达。
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);
我的flex位看起来像这样 - 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.
其中params是一个XML对象。
#1
This is more of the kind of response I was looking for. Thanks to Thierry Boileau for the answer:
这更像是我一直在寻找的那种回应。感谢Thierry Boileau的回答:
You can use two kinds of "XML representations": DomRepresentation and SaxRepresentation. You can instantiate both of them with the posted representation. E.g.: DomRepresentation xmlRep = new DomRepresentation(rep);
您可以使用两种“XML表示”:DomRepresentation和SaxRepresentation。您可以使用已发布的表示来实例化它们。例如:DomRepresentation xmlRep = new DomRepresentation(rep);
The DomRepresentation gives you access to the Dom document. The SaxRepresentation allows you to parse the XML doc with your own contentHandler. See the javadocs here 1 and here 2.
DomRepresentation允许您访问Dom文档。 SaxRepresentation允许您使用自己的contentHandler解析XML文档。请参阅此处的javadoc 1和此处2。
#2
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.
我们目前使用RESTeasy执行此操作,RESTeasy是另一种JAX-RS实现。我们使用JAXB绑定(注释)在XML和我们的模型POJO之间进行映射,并为JAX-RS指定JAXB提供程序,以便它知道如何。我们在Java EE中使用RESTEasy(JAX-RS)文章的RESTful Web服务中描述了这一点,这可能有所帮助。
Update: for Restlet, the JAXB extension might be what you need.
更新:对于Restlet,JAXB扩展可能就是您所需要的。
#3
Through the representation.getText()
method, you can get a String that can be fed into a SAX parser or dom reader.
通过representation.getText()方法,您可以获得可以提供给SAX解析器或dom读取器的String。
#4
@Simon E
I don't understand: which REST implementation for Java are you using?
我不明白:您使用的是哪种REST实现?
So, I just give you an example of using JAX-RS (Jersey implementation)
所以,我给你一个使用JAX-RS(Jersey实现)的例子
The server part (method of some REST class):
服务器部分(某些REST类的方法):
@POST
@Path("/upload")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
public static Response upload(
@FormParam("name") String name,
@FormParam("content") String content)
throws Exception {
// NOTE: you get your content as String
// (do something here)
return Response.ok().build();
}
The client part (method of some JUnit test):
客户端部分(一些JUnit测试的方法):
@Test
public void uploadFile()
throws Exception {
String address = "http://0.0.0.0:8000/r/upload";
WebResource r = Client.create().resource(address);
Form form = new Form();
form.add("name", "test");
form.add("content", "SOME CONTENT GOES HERE");
String s = r.post(String.class, form);
System.out.println(s);
}
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.*
如果您在导入时遇到问题:服务器需要javax.ws.rs。*和javax.ws.rs.core。*客户端需要com.sun.jersey.api.client。*和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
无论如何,我会给你建议使用JAX-RS而不是替代实现,因为JAX-RS将成为即将到来的Java EE 6的一部分
#5
Is this the same procedure even in restlet 2.0??
即使在restlet 2.0中这是相同的程序吗?
I use restlet 2.0m6 and here is the code snippet that I use -
我使用restlet 2.0m6,这是我使用的代码片段 -
@Post
public Representation process(Representation entity)
公共代理过程(代表实体)
{
try
{
DomRepresentation dom = new DomRepresentation(entity);
DomRepresentation dom = new DomRepresentation(entity);
Document d = dom.getDocument();
文件d = dom.getDocument();
.
.
} catch(Exception e)
} catch(例外e)
{ e.printStackTrace(); }
{e.printStackTrace(); }
and it throws a Null Pointer exception at the dom.getDocument() line. Which means no data actually arrived.
它会在dom.getDocument()行抛出一个空指针异常。这意味着没有数据实际到达。
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);
我的flex位看起来像这样 - 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.
其中params是一个XML对象。