I am writing a server application that should forward a file and upload it to another server.
front-end ---> forwarder ---> backend
The forwarder received the file from the frontend successfully. But when I try to forward it, I get an error:
@Autowired
private BackendFeignClient backend;
public ResponseEntity<FileMetadataOutDTO> forward(FileMetadataInDTO fileMetadataInDTO, MultipartFile file, String jwtToken)
{
return backend.save(fileMetadataInDTO, file, jwtToken);
}
The feign client looks like this:
@FeignClient(
name = "file-upload-client", url = "${manager.root.uri}", configuration = BackendFeignClient.MultipartSupportConfig.class)
public interface BackendFeignClient
{
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<FileMetadataOutDTO> save(
@RequestPart("fileMetadataInDTO") FileMetadataInDTO fileMetadataInDTO,
@RequestPart("file") MultipartFile file,
@RequestHeader(HttpHeaders.AUTHORIZATION) String jwtToken);
public class MultipartSupportConfig {
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignFormEncoder () {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
The backend, just like the forwarder, accepts the file and metadata like this:
@PostMapping(consumes = "multipart/form-data")
public FileMetadataOutDTO save(
@Valid @RequestPart(value = "fileMetadataInDTO") FileMetadataInDTO fileMetadataInDTO,
@RequestPart(value = "file", required = true) MultipartFile file)
{
return fileService.save(calibrationCertificateInDTO, file);
}
Unfortunately, I get this error:
Converted error json {
"type" : "about:blank",
"title" : "Bad Request",
"status" : 400,
"detail" : "Required part 'fileMetadataInDTO' is not present.",
"instance" : "/api/v1/upload"
}
I tried adding MediaType.APPLICATION_JSON_VALUE
, but then the body could not be encoded.
consumes = { MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE }
I also added those dependencies:
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.8.0</version>
</dependency>
But it didn’t help.
3
I had a similar issue, where I needed to send
@PostMapping(value = "/save", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
Response save(@RequestPart("file") MultipartFile multipartFile,
@RequestPart("createRequest") CreateRequest createRequest);
so basically you need to define bean JsonFormWriter
in your feign client’s configuration.
public class FeignClientConfiguration {
@Bean
JsonFormWriter jsonFormWriter() {
return new JsonFormWriter();
}
}
This could also help: Feign multipart with Json request part