I have a particular situation with uploading a Map using Spring Cloud Openfeign

I have the following endpoind:

// inside @RestController annotated class
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> upload(@RequestParam Map<String, MultipartFile> upfiles) throws Exception {
    return ResponseEntity.ok(new Object());
}

In another microservice I created a feign client to trigger the above endpoint:

// inside @FeginClient annotated class
@PostMapping(
        value = "/upload",
        produces = MediaType.APPLICATION_JSON_VALUE,
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE
)
ResponseEntity<UploadResponse> uploadDocumentToSign(@Valid @RequestParam Map<String, MultipartFile> upfiles);

The above call fails with a message “Failed to parse multipart servlet request”
So I tried from postman, the only way it works from postman is as follows:
enter image description here

As you can see the Map is placed on the body as a form-data not a query param as it is implied by the method definition

So I tried the same approach with feign client:

//inside @FeginClient annotated class
@PostMapping(
    value = "/upload",
    produces = MediaType.APPLICATION_JSON_VALUE,
    consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
ResponseEntity<UploadResponse> uploadDocumentToSign(@Valid @RequestBody Map<String, 
MultipartFile> upfiles);

But this did not work as well, the request was reaching the server but the Map was empty in the Controller class, even after creating an encoder Bean of type SpringFormEncoder.
It seems that SpringFormEncoder does not know how to handle a multipart/form-data request with a body of type Map<String, MultipartFile> so I had to create my own SpringFormEncoder:

public class DocumentUploadSpringFormEncoder extends SpringFormEncoder {

public DocumentUploadSpringFormEncoder() {
}

public DocumentUploadSpringFormEncoder(Encoder delegate) {
    super(delegate);
}

@Override
public void encode (Object object, Type bodyType, RequestTemplate template) throws EncodeException {
    if (bodyType.equals(MultipartFile[].class)) {
        val files = (MultipartFile[]) object;
        val data = new HashMap<String, Object>(files.length, 1.F);
        for (val file : files) {
            data.put(file.getName(), file);
        }
        super.encode(data, MAP_STRING_WILDCARD, template);
    } else if (bodyType.equals(MultipartFile.class)) {
        val file = (MultipartFile) object;
        val data = singletonMap(file.getName(), object);
        super.encode(data, MAP_STRING_WILDCARD, template);
    } else if (isMultipartFileCollection(object)) {
        val iterable = (Iterable<?>) object;
        val data = new HashMap<String, Object>();
        for (val item : iterable) {
            val file = (MultipartFile) item;
            data.put(file.getName(), file);
        }
        super.encode(data, MAP_STRING_WILDCARD, template);
    } else if (isMultipartFileMap(object)) {
        val map = (Map<?, ?>) object;
        val entrySet = map.entrySet();
        val data = new HashMap<String, Object>();
        for (Map.Entry entry : entrySet) {
            val file = (MultipartFile) entry.getValue();
            val key = (String) entry.getKey();
            data.put(key, file);
        }
        super.encode(data, MAP_STRING_WILDCARD, template);
    }
    else {
        super.encode(object, bodyType, template);
    }
}

private boolean isMultipartFileCollection (Object object) {
    if (!(object instanceof Iterable<?> iterable)) {
        return false;
    }
    val iterator = iterable.iterator();
    return iterator.hasNext() && iterator.next() instanceof MultipartFile;
}

private boolean isMultipartFileMap (Object object) {
    if (!(object instanceof Map<?, ?> map)) {
        return false;
    }
    val entrySet = map.entrySet();
    for (Map.Entry entry : entrySet) {
        if (!(entry.getValue() instanceof MultipartFile)) {
            return false;
        }
    }
    return true;
 }
}

And only now everything worked.
My questions are:

  1. Is this the way to accept a Map<String, MultipartFile> @RequestParam Map<String, MultipartFile> upfiles, why @RequestParam when you actually need to put this in the body ?
  2. If it is the correct why you define it one way in the Controller and another way on the client side useing spring openfegin (still pring framework…)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật