spring-cloud-openfeign icon indicating copy to clipboard operation
spring-cloud-openfeign copied to clipboard

Form requests with parameters of map type are not supported

Open brucelwl opened this issue 7 months ago • 1 comments

The following is a feign interface I defined, I want to receive all the form request parameters through Map, which can run correctly in springmvc

@PostMapping(path = "/requestMap", consumes = "application/x-www-form-urlencoded")
Store requestMap(@RequestParam Map<String, String> params);

but I found that when feign client requests, the parameters are placed on the URL, not in the body of the FORM

brucelwl avatar Jun 19 '25 12:06 brucelwl

Just make the following adjustments to solve the problem

 if (Map.class.isAssignableFrom(parameterType)) {
    if (isPostOrPutForm(method)) {
        checkState(data.queryMapIndex() == null, "Query map can only be present once.");
        data.bodyIndex(parameterIndex);
        data.bodyType(parameterType);
        data.template().header(HttpEncoding.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE);
        return true;
    }
}

 private boolean isPostOrPutForm(Method method) {
    Set<RequestMapping> requestMappings = AnnotatedElementUtils.findAllMergedAnnotations(method, RequestMapping.class);
    for (RequestMapping requestMapping : requestMappings) {
        if (isPostOrPutFormMapping(requestMapping)) {
            return true;
        }
    }
    return false;
}

// @RequestMapping + @RequestParam + Map, POST 或者 PUT 默认为FORM请求
private boolean isPostOrPutFormMapping(RequestMapping requestMapping) {
    for (RequestMethod httpMethod : requestMapping.method()) {
        if (httpMethod == RequestMethod.POST || httpMethod == RequestMethod.PUT) {
            return true;
        }
    }
    return false;
}

brucelwl avatar Jun 19 '25 12:06 brucelwl