如何使用 Jetty 客户端 API 发送 JAXB 编组对象作为参数?

发布于 2024-11-25 06:55:08 字数 2011 浏览 0 评论 0原文

...实际上,我正在寻找 REST 样式的 EJB 或 SOAP 替换;)


这使我能够从服务器接收 JAXB 编组对象

客户端

WebResource r = client.resource("http://localhost:9999/resource1");

SomeObject in = r.post(SomeObject.class);

服务器端

@Path("/")
public static final class TestResource {
    @Path("resource1")
    @POST
    public SomeObject resource1() {
        return new SomeObject("Object1");
    }
}

以下是如何发送 JAXB 编组对象的示例。 ..

...当它是唯一(未命名)参数时。
我什至不知道这种行为是否有效。有一点不能这样工作:当客户端使用 client.addFilter(new GZIPContentEncodingFilter()) 时,服务器无法理解该请求,即使所有其他(通常)gzip 压缩的请求都正常。

客户端

WebResource r = client.resource("http://localhost:9999/resource2");

SomeObject out = new SomeObject("no name");

SomeObject in = r.post(SomeObject.class, out /*!!!*/);

服务器端

@Path("/")
public static final class TestResource {
    @Path("resource2")
    @POST
    public SomeObject resource2(SomeObject o) {
        o.setName("NEW NAME!"); // "modify" object
        return o; // send back
    }
}

同样,这种行为似乎不一致,甚至不打算工作。否则为什么 gzip 内容编码过滤器会在这里失败? 任何人都可以对此发表评论吗?


但是我如何发送这样一个对象作为请求参数

客户端

WebResource r = client.resource("http://localhost:9999/resource3");

SomeObject out = new SomeObject("no name"); // this would be the sent param
r. /* some magic method to add a JAXB-marshalled object as parameter */ (out);

SomeObject in = r.post(SomeObject.class); // this receives the "modified" object

服务器端

@Path("/")
public static final class TestResource {
    @Path("resource3") // ??? have something to happen to the URI?
    @POST
    // which kinds of Param? Path? Query? Form? Matrix? Something else?
    public SomeObject resource3(@PathParam("a") SomeObject o) {
        o.setName("NEW NAME!"); // "modify" object
        return o; // send back
    }
}

...practically I'm looking for a REST-style EJB-or-SOAP-replacement ;)


This enables me to receive an JAXB-marshalled object from server

Client side

WebResource r = client.resource("http://localhost:9999/resource1");

SomeObject in = r.post(SomeObject.class);

Server side

@Path("/")
public static final class TestResource {
    @Path("resource1")
    @POST
    public SomeObject resource1() {
        return new SomeObject("Object1");
    }
}

Here's an example how to send a JAXB-marshalled object...

...when it's the only (unnamed) parameter.
I don't even know if this behavior is intended to work. One thing doesn't work this way: when the client uses client.addFilter(new GZIPContentEncodingFilter()), the server doesn't understand the request, even when all other (usual) gzipped requests are fine.

Client side

WebResource r = client.resource("http://localhost:9999/resource2");

SomeObject out = new SomeObject("no name");

SomeObject in = r.post(SomeObject.class, out /*!!!*/);

Server side

@Path("/")
public static final class TestResource {
    @Path("resource2")
    @POST
    public SomeObject resource2(SomeObject o) {
        o.setName("NEW NAME!"); // "modify" object
        return o; // send back
    }
}

Again, this behavior seems not consistent or even not intended to work. Why else should the gzip content encoding filter fail here? Can anyone comment on this?


But how can I send such an object as a request parameter?

Client side

WebResource r = client.resource("http://localhost:9999/resource3");

SomeObject out = new SomeObject("no name"); // this would be the sent param
r. /* some magic method to add a JAXB-marshalled object as parameter */ (out);

SomeObject in = r.post(SomeObject.class); // this receives the "modified" object

Server side

@Path("/")
public static final class TestResource {
    @Path("resource3") // ??? have something to happen to the URI?
    @POST
    // which kinds of Param? Path? Query? Form? Matrix? Something else?
    public SomeObject resource3(@PathParam("a") SomeObject o) {
        o.setName("NEW NAME!"); // "modify" object
        return o; // send back
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文