1
我运行宣布一些像这样的JAX-RS资源:如何将自定义Content-Type设置为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客户端(或任何其他客户端,如果它有助于解决我的问题)来测试他们:
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。
哦,我不能改变服务声明。
您认为测试此资源的最佳方式是什么?
谢谢安德鲁。认为这不是一个太糟糕的解决方案。 – romixch