Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Multipart file from one service to another service in spring boot?

I would like to pass multipart file from one service to another.

Client --> Service1 --> Service2

This shows an error "500 internal server error, Current request is not a multipart request" when I pass the file from Service1 to Service2

Client --> Service2 when I send the file directly its working but not through Service1

I want to know what could be the reason, I guess I am missing some header parts when passing the multipart file as parameter.

Service2

@PostMapping(path="/upload")
public ResponseEntity<Properties> upload(@RequestParam("file") MultipartFile multiPart) {
    return saveFile(multiPart);
}

Service2-client

@FeignClient
(name="${feign.upload.serverId}", configuration = UploadServiceClientConfiguration.class, decode404 = true)
public interface UploadServiceClient {

    @PostMapping(path="/upload")
    ResponseEntity<Properties> upload(@RequestParam("file") MultipartFile multiPart);

    class UploadServiceClientConfiguration {
        @Value(value="${feign.upload.user}")
        String user;
        @Value(value="${feign.upload.password}")
        String password;
        @Bean
        public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
            return new BasicAuthRequestInterceptor(user, password);
        }
    }
}

Service1

@Autowired
UploadServiceClient uploadSvcClient;
@PostMapping(path="/upload")
public ResponseEntity<Properties> uploadAttachment(@RequestParam("file") MultipartFile file) {
    return uploadSvcClient.upload(file);
}
like image 489
Gopikrishna Mandapati Avatar asked Oct 20 '25 09:10

Gopikrishna Mandapati


1 Answers

At last able to solve the communication issue to the another service using the post File upload spring cloud feign client

I have changed the FeignClient parameter type from

@RequestParam("file") MultipartFile mFile

to

@RequestPart("file") MultiValueMap file.

FeignClient Signature

@PostMapping(value="/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        ResponseEntity<Properties> upload(@RequestHeader(name=UID,required=false) String uid, @RequestPart("file") MultiValueMap<String, Object> file);

Service1 Implementation

@PostMapping(path="/upload")
    public ResponseEntity<Properties> uploadAttachment(@RequestHeader(IRSConsts.UID) String uid, @RequestParam("file") MultipartFile mFile) {
        MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
        ByteArrayResource contentsAsResource = null;
        try {
            contentsAsResource = new ByteArrayResource(mFile.getBytes()) {
                @Override
                public String getFilename() {
                    return mFile.getOriginalFilename();
                }
            };
        } catch (IOException e) {
            e.printStackTrace();
        }
        multiValueMap.add("file", contentsAsResource);
        return transSvcClient.upload(uid, multiValueMap);
    }

Service2 Implementation

@PostMapping(path = "/upload")
    @Headers("Content-Type: multipart/form-data")
    public ResponseEntity<Properties> upload(@RequestHeader(name = UID, required = false) String uid,
            @RequestPart("file") MultipartFile multiPart) {
        //Save Attachment.
    }
like image 129
Gopikrishna Mandapati Avatar answered Oct 22 '25 01:10

Gopikrishna Mandapati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!