Search code examples
javaspring-bootfeignkaizala

how can i send system file to another api using @FeginClient


I have to send System file to my other service using @FeignClient.

@RequestMapping(method = RequestMethod.POST, path = "v1/media")
@Headers({"Content-Type: multipart/form-data"})
void uploadMedia(@RequestHeader("refreshToken") String refreshToken, @RequestPart File FileName);

Error:

[
    {
        "message":"No file to upload!", 
        "errorCode":"InvalidParameters", 
        "errorCategoryKey":"InvalidParametersException"
    }
]

Solution

  • i used the RestTemplate for the upload the Image and it's working.

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.set("accessToken", accessToken);
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", new FileSystemResource(new File("File.png")));
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        String serverUrl = "/v1/media";
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<MediaResource> response = restTemplate.postForEntity(serverUrl, requestEntity, MediaResource.class);
        System.out.println("Response code: " + response);