0

I need to upload a MultipartFile to a third-party service via my own backend service. The parameter in the multipart form is 'nameA' but the third-party service need its param name is 'nameB'.

Normally I can solve it in two ways:

  1. Change the param name of frontend to 'nameB'
  2. Change the param name of MultipartFile to 'nameB' in backend service.

But I cannot change the frontend now, so I want to finger out how to modify the param name of MultipartFile in backend service.

The controller of backend service is:

@PostMapping("/url")
public Response method(@RequestParam("nameA") MultipartFile file) {
    return Service.method(file);
}

In Feign Client for uploading file to third-party service:

@PostMapping(value = "/url1/url2", consumes = MULTIPART_FORM_DATA_VALUE)
Response method(@RequestParam("nameB") MultipartFile file);

However the use of specify the param with @RequestParam doesn't work.

So does anyone know how to modify the param name of MultipartFile? Thanks a lot!

Sid
  • 31
  • 4

2 Answers2

0

That is completely unrelated to your controllers spring annotations and instead depends on how you would upload that file to the 3rd party service. Since you mentioned uploading it, I assume you need to create a new HTTP multipart request in your backend service that would upload the file to the 3rd party service. When creating that request, you will be able to specify the name of the multipart part.

Richard
  • 1,543
  • 1
  • 9
  • 13
  • Hi, I understand that the spring annotation of @RequestParam is used to filter not to set now. Thank you, Is there any instruction for create http multipart request in feign client? I just pass the multipart file to feign client before. – Sid Oct 24 '18 at 02:51
  • I must admit, that I don't know. I am only guessing here... If it is not working for you, ensure that you are using the latest version of the spring Feign thingy, cause they had some problems with their custom annotations on feign: https://github.com/spring-cloud/spring-cloud-netflix/issues/1201 If you are using the latest and greatest I'd try the plain feign approach mentioned here: https://stackoverflow.com/questions/31752779/file-upload-using-feign-multipart-form-data – Richard Oct 24 '18 at 03:06
0

You can set a name of the MultipartFile in the FeignClient as you need, this is a sample from my project:

Сontroller API (receiving side):

@RestController
@RequestMapping("/files")
public class FilesController {    

    @PostMapping(path = "/upload")
    @ResponseStatus(HttpStatus.CREATED)
    public FileDescriptor upload(@RequestPart(value = "data") MultipartFile multipartFile) {
       ...
    }
}

Feign client (sending side):

@FeignClient(value = "file-service", configuration = FeignConfig.class)
public interface ContentStorageFeign {

    @ResponseBody
    @PostMapping(value = "/files/upload", produces = MediaType.APPLICATION_JSON_VALUE)
    FileDescriptor create(@RequestPart(value = "data") MultipartFile multipartFile);
}

And this is my FeignConfig:

@Configuration
public class FeignConfig {

    @Bean
    public Decoder decoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        return new ResponseEntityDecoder(new SpringDecoder(messageConverters));
    }

    @Bean
    public Encoder encoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

But if you need to create a new request(from a file received from somewhere) and rename this file before sending, this is another problem.