开发者

How can I send a JAXB-marshalled object as a parameter using the Jetty client API?

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


This enable开发者_如何学编程s 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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜