2016-10-10 44 views
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。

哦,我不能改變服務聲明。

您認爲測試此資源的最佳方式是什麼?

回答

1

使用您的自定義MediaType創建實體,然後向客戶端註冊自定義MessageBodyWriter。這可能與當調用isWritable時擴展現有MessageBodyWrite以返回true一樣簡單,或者您可能必須實現writeTo方法。

+0

謝謝安德魯。認爲這不是一個太糟糕的解決方案。 – romixch

相關問題