2016-07-20 173 views
0

我需要使用上傳文件API。我使用Jersy創建了客戶端,它給了我適當的迴應。這裏是示例代碼:Jersy Rest客戶端到Apache CXF客戶端轉換

final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build(); 
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("somePath/fileName.txt")); 
FormDataMultiPart formDataMultiPart = new FormDataMultiPart(); 
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("someField", "001").bodyPart(filePart); 
final WebTarget target = client.target("http://serverUrl.com:8000/cq5/uploadApi"); 
final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType())); 

但在我的項目中,我本來應該使用CXF,我試圖用CXF實現同樣的事情。這是我嘗試過的。

String path = "/uploadApi"; 
WebClient topicWebClient = WebClient.fromClient(webClient, true) 
     .type(MediaType.MULTIPART_FORM_DATA).path(path); 
ContentDisposition cd = new ContentDisposition("attachment;filename=fileName.txt"); 
Attachment att = new Attachment("file", new ByteArrayInputStream("testContent".getBytes()), cd); 
final Response response = topicWebClient.post(att); 

但在這裏我沒有得到任何迴應。它繼續加載。即使在我的日誌中也沒有得到任何錯誤。

我錯過了什麼?請幫助我得到適當的迴應。

+0

你是怎麼創建'webClient'的?似乎網址不正確 – pedrofb

+0

沒有網址是正確的。我沒有在該代碼片段中添加該代碼的詳細信息。 – Ramesh

+0

什麼是響應代碼?服務器是否響應? – pedrofb

回答

1

[在評論答案]

服務器響應客戶CXF 401不正當,所以它是一種身份驗證問題。與Jersey客戶端進行比較後,需要在請求頭中添加服務器所需的憑據。

這些字段不在CXF的標題中,所以很可能是原因。在配置CXF WebClient時添加它們。

webClient.header(name,value)