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