【发布时间】:2020-08-06 11:21:16
【问题描述】:
我有一个使用代理接口实现的 RESTEasy 客户端 (3.6.2.Final),我想在其中调用 WS 来上传文件。如果我定义一个修复 Content-Type,调用会正常工作,但如果我想自己设置一个,调用会失败。
界面:
@POST
@Path("api/files/upload/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes()
FileUploadResponse uploadFile(byte[] file);
在实例化代理时,我提供了基本身份验证:
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath("http://..."));
target.register(new BasicAuthentication("username", "password"));
ApiInterface api = target.proxy(ApiInterface.class);
像这样调用有效,但标头中的 Content-Type 是通用的 * / *
如果我像这样添加 HeaderParam,调用将失败并显示 HTTP 400(错误请求):
@POST
@Path("api/files/upload/")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes()
FileUploadResponse uploadFile(byte[] file, @HeaderParam("Content-Type") String contentType);
我检查了 HTTP 调用,并且 Content-Type 在那里出现了两次:一次是 * / *,另一次是“application/pdf”(我在调用 uploadFile 时提供的那个)。
我以为我可以删除@Consumes(),因为我自己设置了标题,但后来又出现了另一个错误:
java.lang.RuntimeException: RESTEASY004590: You must define a @Consumes type on your client method or interface, or supply a default
根据我发送的文档,每次调用的 Content-Type 都会有所不同。 RESTEasy中没有办法将其定义为参数吗?
【问题讨论】:
标签: java wildfly jax-ws resteasy