Skip to content

fix: issues/1211, support Map parameter FORM request #1212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import feign.MethodMetadata;

import org.springframework.cloud.openfeign.AnnotatedParameterProcessor;
import org.springframework.cloud.openfeign.encoding.HttpEncoding;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import static feign.Util.checkState;
Expand Down Expand Up @@ -53,6 +59,12 @@ public boolean processArgument(AnnotatedParameterContext context, Annotation ann

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

return true;
Expand All @@ -69,4 +81,25 @@ public boolean processArgument(AnnotatedParameterContext context, Annotation ann
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;
}

}