如何使用 Jersey 客户端发布 XML 文件?

发布于 2024-12-21 14:55:52 字数 485 浏览 1 评论 0原文

    final WebResource service = client.resource(UriBuilder.fromUri(WSURI).build());
    service.type(MediaType.APPLICATION_XML);
    service.accept(MediaType.TEXT_PLAIN);
    final Builder builder = service.header(HttpHeaders.AUTHORIZATION, HEADER);

    File file = new File("/test.xml");
    builder.entity(file);
    final ClientResponse response = builder.post(ClientResponse.class);

我想发送一个 XML 文件并接收返回的响应。我正在尝试的代码给出了 400 BAD 请求,请有人帮忙。我不确定这里出了什么问题。

    final WebResource service = client.resource(UriBuilder.fromUri(WSURI).build());
    service.type(MediaType.APPLICATION_XML);
    service.accept(MediaType.TEXT_PLAIN);
    final Builder builder = service.header(HttpHeaders.AUTHORIZATION, HEADER);

    File file = new File("/test.xml");
    builder.entity(file);
    final ClientResponse response = builder.post(ClientResponse.class);

I want to send an XML file and receive the response back. The code that I am trying gives 400 BAD request, please could someone help. I am not sure what is going wrong here.

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

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

发布评论

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

评论(1

三生路 2024-12-28 14:55:52

WebResource 是不可变的 - 它的方法返回一个新的构建器实例。因此,代码片段的第二行和第三行没有效果,因为您忽略了它们的结果。调用 entity() 方法时也是如此。您应该执行以下操作:

final WebResource service = client.resource(UriBuilder.fromUri(WSURI).build());
Builder builder = service.type(MediaType.APPLICATION_XML);
builder = builder.accept(MediaType.TEXT_PLAIN);
builder = builder.header(HttpHeaders.AUTHORIZATION, HEADER);

File file = new File("/test.xml");
builder = builder.entity(file);
final ClientResponse response = builder.post(ClientResponse.class);

WebResource is immutable - it's methods return a new builder instance. So, the 2nd and 3rd line of your code snippet have no effect, since you ignore their result. Same when you call the entity() method. You should do the following instead:

final WebResource service = client.resource(UriBuilder.fromUri(WSURI).build());
Builder builder = service.type(MediaType.APPLICATION_XML);
builder = builder.accept(MediaType.TEXT_PLAIN);
builder = builder.header(HttpHeaders.AUTHORIZATION, HEADER);

File file = new File("/test.xml");
builder = builder.entity(file);
final ClientResponse response = builder.post(ClientResponse.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文