tony
tony

Reputation: 31

calling spring-based REST api with Jersey client

Here is the spring-based api:

@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);
    }

Here's my best effort:

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);

Response is 404.
2 questions: - Is the spring API defined properly? - How do I use Jersey client to call the api properly?

Upvotes: 2

Views: 660

Answers (1)

tony
tony

Reputation: 31

OK, I got it working, using the following code:

        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);

I needed to use the "bodyPart" method, which doesn't seem to work in the 'builder' pattern.

Upvotes: 1

Related Questions