REST Web 服务使用 Restlet 接受 POST - 最佳实践

发布于 2024-07-12 23:57:01 字数 384 浏览 4 评论 0原文

我有我的资源和他们典型的重写方法来处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

再见回来 2024-07-19 23:57:01

这更像是我一直在寻找的回应。 感谢 Thierry Boileau 的回答:

您可以使用两种“XML
表示”:DomRepresentation
和萨克斯表示法。 你可以
实例化它们两个
发布代表。 例如:
DomRepresentation xmlRep = 新
DomRepresentation(rep);

DomRepresentation 为您提供访问权限
到 Dom 文档。 这
SaxRepresentation 允许您解析
XML 文档与您自己的
内容处理程序。 请参阅此处的 javadoc
1 和此处 2。

  1. http://www.restlet.org/documentation/1.1/api/org/restlet/res ource/DomRepresentation.html

  2. http://www.restlet.org/documentation/1.1 /api/org/restlet/resource/SaxRepresentation.html

This is more of the kind of response I was looking for. Thanks to Thierry Boileau for the answer:

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

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.

  1. http://www.restlet.org/documentation/1.1/api/org/restlet/res​ource/DomRepresentat​ion.html

  2. http://www.restlet.o​rg/documentation/1.1​/api/org/restlet/res​ource/SaxRepresentat​ion.html

顾北清歌寒 2024-07-19 23:57:01

我们目前使用 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.

耀眼的星火 2024-07-19 23:57:01

通过 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.

李不 2024-07-19 23:57:01

@Simon E

我不明白:您使用的是哪种 Java REST 实现?

的示例

所以,我只是给你一个使用 JAX-RS (Jersey 实现) 服务器部分(某些 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();
}

客户端部分(某些 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);
}

就是这样!

如果您在导入时遇到问题:
服务器需要 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):

@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):

@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.*

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

亣腦蒛氧 2024-07-19 23:57:01

即使在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文