[英]How can I set a custom Content-Type to jax-rs client?
我正在运行一些声明如下的 JAX-RS 资源:
public interface MyEntityServiceV2 {
@PUT
@Consumes({"application/myentity-v2+json"})
@Produces({"application/myentity-v2+json"})
@Path("/{uuid:[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}}")
Response putMyEntity(@PathParam("uuid") String uuid, MyEntityDto myEntity);
}
现在我想使用 Jersey-Client(或任何其他客户端,如果它有助于解决我的问题)来测试它们:
MyEntityDto dto = new MyEntityDto();
Entity<MyEntityDto> entity = Entity.entity(dto, MediaType.APPLICATION_JSON_TYPE);
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:7001/path/" + UUID.randomUUID().toString());
Invocation.Builder builder = target.request("application/myentity-v2+json");
builder.header("Content-Type", "application/myentity-v2+json");
Response response = builder.put(entity);
不幸的是,这不起作用。 请求始终包含 application/json 的内容类型。 这与服务器上的接口不匹配。
作为替代方案,我可以使用我的自定义 MediaType 创建实体,这也会导致错误,因为它找不到该类型的 MessageBodyWriter。
哦,我无法更改服务声明。
您认为测试此资源的最佳方法是什么?
使用您的自定义 MediaType 创建一个实体,然后向您的客户端注册一个自定义 MessageBodyWriter。 它可以像扩展现有的 MessageBodyWrite 以在isWritable
时返回true
一样简单,或者您可能必须实现writeTo
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.