I have a Spring Boot 2.4.3 with
@RestController
public class DemoFeignController {
@PutMapping(value = "/document", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String upload(@RequestPart("file") MultipartFile multipartFile,
@RequestPart("config") String config) {
System.out.println(multipartFile.getSize());
System.out.println("config:" + config);
return "upload";
}
@PutMapping(value = "/document2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String upload2(@RequestPart("file") MultipartFile multipartFile,
@RequestPart("config") DocumentConfiguration documentConfiguration) {
System.out.println(multipartFile.getSize());
System.out.println("config2:" + documentConfiguration);
return "upload2";
}
}
I also have
@Component
public class DemoFeignServiceAdapter extends WebMvcConfigurerAdapter {
@Autowired
DemoFeignServiceInterceptor filetnetServiceInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(filetnetServiceInterceptor);
}
}
and
@Component
public class DemoFeignServiceInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("Pre Handle http request");
request.getParts().stream()
.forEach(part -> {
System.out.println(part.getName());
});
return true;
}
}
and eventually
@Data public class DocumentConfiguration { private String name; private String type; }
If I call those 2 endpoints with Postman, it's working.
With
Calling /upload and /upload2, I can see in the logs
Pre Handle http request
file
config
1161530
config:{"name": "my-document", "type": "pdf"}
Pre Handle http request
file
config
1161530
config2:DocumentConfiguration(name=my-document, type=pdf)
config2:DocumentConfiguration(name=my-document, type=pdf)
Now with Feign client
@FeignClient(name = "demo-service", url = "http://localhost:8090")
public interface DemoFeignProxy {
@PutMapping(value = "/document", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String upload(@RequestPart("file") MultipartFile multipartFile,
@RequestPart("config") String config);
@PutMapping(value = "/document2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
String upload2(@RequestPart("file") MultipartFile multipartFile,
@RequestPart("config") DocumentConfiguration documentConfiguration);
}
and
@SpringBootTest
class DemoFeignApplicationTests {
@Autowired
DemoFeignProxy demoFeignProxy;
@Test
void upload() {
byte[] file = "Hello world".getBytes(StandardCharsets.UTF_8);
MultipartFile multipartFile = new MockMultipartFile("file", "temp", "text/plain", file);
System.out.println(demoFeignProxy.upload(multipartFile, "my-config"));
}
@Test
void upload2() {
byte[] file = "Hello world".getBytes(StandardCharsets.UTF_8);
MultipartFile multipartFile = new MockMultipartFile("file", "temp", "text/plain", file);
DocumentConfiguration documentConfiguration = new DocumentConfiguration();
documentConfiguration.setName("my-config");
documentConfiguration.setType("pdf");
System.out.println(demoFeignProxy.upload2(multipartFile, documentConfiguration));
}
}
For upload, it's working.
Pre Handle http request
file
config
11
config:my-config
But for upload2, it's not working
Pre Handle http request
file
name
type
2021-03-02 13:39:05.850 WARN 20836 --- [nio-8090-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver :
Resolved [org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'config' is not present]
I don't understand why I have name and type instead of config.
Because it's fine with Postman, I suppose the problem is coming from the Feign client. But I don't know what I'm missing.
Edit:
Calling http://127.0.0.1:8090/swagger-ui/index.html I also have an exception
javax.servlet.ServletException: org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null