2012-12-04 56 views
1

這裏REST API是基於彈簧的API:調用基於彈簧的使用Jersey客戶端

@RequestMapping(value = ControllerConstants.FILE_UPLOAD_URI, method = RequestMethod.POST) 
    public @ResponseBody 
    boolean processUpload(@RequestParam("file") MultipartFile file, @RequestParam("accessionId") String accessionId, @RequestParam("protocol") String protocol, HttpServletRequest request) throws IOException {   
     logger.info("upload file: {} with accessionId: {} and protocol:{}",file.getOriginalFilename(),accessionId,protocol);   
     return dataManagerService.writeFile(file, protocol, accessionId); 
    } 

這是我最大的努力:

FormDataMultiPart part = new FormDataMultiPart() 
       .field("accessionId", returnedAccessionId) 
        .field("protocol", protocol) 
        .field("name", file.getName()) 
        .field("file", stream, MediaType.APPLICATION_OCTET_STREAM_TYPE);  

      MultivaluedMap<String, String> params = new MultivaluedMapImpl(); 
      params.add("file", file.getAbsolutePath()); 
      params.add("accessionId", returnedAccessionId); 
      params.add("protocol", protocol);  

      WebResource resource2 = client.resource(agent.getServerEndpointUri() + "/novax/service/dataManager/upload"); 
      ClientResponse response4 = resource2 
        .queryParams(params) 
        .type(MediaType.MULTIPART_FORM_DATA_TYPE) 
        .post(ClientResponse.class, part); 

響應是404
2個問題: - Spring API是否正確定義? - 如何使用Jersey客戶端正確調用API?

回答

0

好了,我懂了工作,使用下面的代碼:

 FormDataMultiPart part = new FormDataMultiPart() 
       .field("accessionId", returnedAccessionId) 
       .field("protocol", protocol) 
       .field("name", file.getName()); 
     part.bodyPart (new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE)); 

     WebResource resource2 = client.resource(pathOrUrl); 
     ClientResponse response4 = resource2 
       .type(MediaType.MULTIPART_FORM_DATA) 
       .post(ClientResponse.class, part); 

我需要使用「正文部分」的方法,這似乎並沒有在「建設者」模式工作。

相關問題