2016-01-22 96 views
0

Dropwizard(版本0.8.2)在內部使用Jersey來提供HTTP客戶端。我正在使用此客戶端將多部分POST請求發送到外部Rest Rest Endpoint到SMS服務。代碼如下,但它似乎並沒有工作,因爲我沒有收到任何消息通過這種方法也不會引發任何錯誤。 URI用於第一抽樣http://enterprise.com/GatewayAPI/rest?userid=%s&password=%s&method=xlsUpload&filetype=zip&msg_type=TEXT&auth_scheme=PLAIN&v=1.1Dropwizard Jersey客戶端多部分http請求

FileDataBodyPart fileDataBodyPart = new FileDataBodyPart(fileName, file, 
                 MediaType.APPLICATION_OCTET_STREAM_TYPE); 
     FormDataMultiPart multiPart = new FormDataMultiPart(); 

     multiPart.field("fileName", fileName).bodyPart(fileDataBodyPart); 
     Entity<FormDataMultiPart> entity = 
      Entity.entity(multiPart, multiPart.getMediaType());// MediaType.MULTIPART_FORM_DATA_TYPE) 

     Client tenacityClient = TenacityJerseyClientBuilder 
      .builder(AppDependencyKeys.BULK_SMS) 
      .usingTimeoutPadding(Duration.milliseconds(500)).build(client) 
      .register(MultiPartFeature.class); 

     Invocation invocation = getResourceBuilder(tenacityClient, uri).buildPost(entity); 
     Future<Response> futureResponse = invocation.submit(); 
     long start = System.currentTimeMillis(); 

     futureResponse.get(); 

但是,當我使用Apache Commons的HttpClient下面方法相同的作品。下面給出了相同的工作代碼。

HttpClient client = new HttpClient(); 
    PostMethod method = new 
     PostMethod("http://enterprise.com/GatewayAPI/rest"); 
    Part[] parts = { 
     new StringPart("method", "xlsUpload"), 
     new StringPart("userid", "*******"), 
     new StringPart("password", "*******"), 
     new StringPart("filetype", "zip"), 
     new StringPart("v", "1.1"), 
     new StringPart("auth_scheme", "PLAIN"), 
     new FilePart(file.getName(), file) 
    }; 
    method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); 
    int statusCode = client.executeMethod(method); 
    log.info("Status code: {}", statusCode); 

但我想使用第一種方式,因爲它更適合我的基礎設施。

+0

問題是什麼? – eg04lt3r

+0

問題是爲什麼第一個代碼不工作而第二個工作正常? – pradex

回答

0

我認爲你應該爲實體設置適當的媒體類型。目前,您創建了新的FormDataMultiPart,但是,您沒有設置和媒體類型,並且它使用「text/plain」默認值。

因此,您應該將MediaType.APPLICATION_OCTET_STREAM_TYPE設置爲您的FormDataMultiPart作爲媒體類型。

+1

我嘗試過,但我得到以下異常。 '處理請求時捕獲異常javax.ws.rs.ProcessingException異常:org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:MessageBodyWriter not found for media type = application/octet -stream,type = class org.glassfish.jersey.media.multipart.FormDataMultiPart,genericType = class org.glassfish.jersey.media.multipart.FormDataMultiPart。' – pradex

相關問題