调用RESTEasy客户端代理接口,如何指定端点将使用哪种内容类型?

时间:2015-05-27 19:27:14

标签: java jax-rs resteasy apache-tika

我想通过二进制文件PUT到一个可以使用许多可能的mimetypes之一的端点。具体来说,我正在与Apache Tika服务器进行通信,该服务器可以采用PDF或Word .docx文件。

我已经设置了一个客户端代理接口,我可以硬编码,例如.docx mimetype:

public interface TikaClient {
    @PUT
    @Path("tika")
    @Consumes("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    Response putBasic(byte[] body);
}

当我打电话时,这将有效:

ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(url);
TikaClient tikaClient = target.proxy(TikaClient.
Response response = tikaClient.putBasic(binaryFileData);

....但该端点也可以采用“text / plain”或“application / pdf”。

我相信我可以指定多个@Consumes选项:@Consumes({"text/plain", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"})

但它似乎没有选择正确的,我不知道如何告诉它有问题的文件是哪一个。

1 个答案:

答案 0 :(得分:2)

您可以添加多个MediaTypes,如上所述。如果服务器接受多个MediaType,则必须与客户端协商应该使用哪个MediaType。客户端应发送Content-Type标头Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document

要在代理框架中添加Content-Type标头,您必须选择:

  • @HeaderParam("Content-Type")参数添加到putBasic方法。
  • 注册将设置此标题的ClientRequestFilter

我不知道为什么但是使用代理框架这对我不起作用。如果您使用标准客户端方式,它可以正常工作:

client.target(url)
.request()
.put(Entity.entity(binaryFileData,
  MediaType.valueOf("application/vnd.openxmlformats-officedocument.wordprocessingml.document")));

服务器现在应该选择您正在使用的MediaType。