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

时间:2011-12-15 12:44:40

标签: java jersey

    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请求,请有人帮忙。我不确定这里出了什么问题。

1 个答案:

答案 0 :(得分:2)

WebResource是不可变的 - 它的方法返回一个新的构建器实例。因此,您的代码段的第2行和第3行无效,因为您忽略了它们的结果。调用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);