ParseHandlersByName
ParseHandlersByName是Feign解析接口定义的方法的实现,在Feign.Builder的build()方法中会初始化一个它的实例传递给ReflectiveFeign,以便在生成代理对象时通过ParseHandlersByName类的实例来解析接口方法.
如下代码片段:
public Feign build() {
SynchronousMethodHandler.Factory synchronousMethodHandlerFactory =
new SynchronousMethodHandler.Factory(client, retryer, requestInterceptors, logger,
logLevel, decode404, closeAfterDecode);
ParseHandlersByName handlersByName =
new ParseHandlersByName(contract, options, encoder, decoder,
errorDecoder, synchronousMethodHandlerFactory);
return new ReflectiveFeign(handlersByName, invocationHandlerFactory);
}
ParseHandlersByName.apply()
这个是解析接口方法的具体实现,如下代码片段:
public Map<String, MethodHandler> apply(Target key) {
List<MethodMetadata> metadata = contract.parseAndValidatateMetadata(key.type());
Map<String, MethodHandler> result = new LinkedHashMap<String, MethodHandler>();
for (MethodMetadata md : metadata) {
BuildTemplateByResolvingArgs buildTemplate;
if (!md.formParams().isEmpty() && md.template().bodyTemplate() == null) {
buildTemplate = new BuildFormEncodedTemplateFromArgs(md, encoder);
} else if (md.bodyIndex() != null) {
buildTemplate = new BuildEncodedTemplateFromArgs(md, encoder);
} else {
buildTemplate = new BuildTemplateByResolvingArgs(md);
}
result.put(md.configKey(),
factory.create(key, md, buildTemplate, options, decoder, errorDecoder));
}
return result;
}
分析如下:
- 1.通过Contract对象解析方法元数据
- 2.根据方法参数获取HTTP请求构建模板
- 3.根据方法生成HTTP调用代理类
Contract:方法解析
Contract接口只定义了一个解析元数据的方法:
List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType);
Contract中的内部类:BaseContract
BaseContract是一个实现了Contract接口的抽象类,抽象了一些具体的解析方法,如下:
@Override
public List<MethodMetadata> parseAndValidatateMetadata(Class<?> targetType) {
checkState(targetType.getTypeParameters().length == 0, "Parameterized types unsupported: %s",
targetType.getSimpleName());
checkState(targetType.getInterfaces().length <= 1, "Only single inheritance supported: %s",
targetType.getSimpleName());
if (targetType.getInterfaces().length == 1) {
checkState(targetType.getInterfaces()[0].getInterfaces().length == 0,
"Only single-level inheritance supported: %s",
targetType.getSimpleName());
}
Map<String, MethodMetadata> result = new LinkedHashMap<String, MethodMetadata>();
for (Method method : targetType.getMethods()) {
if (method.getDeclaringClass() == Object.class ||
(method.getModifiers() & Modifier.STATIC) != 0 ||
Util.isDefault(method)) {
continue;
}
MethodMetadata metadata = parseAndValidateMetadata(targetType, method);
checkState(!result.containsKey(metadata.configKey()), "Overrides unsupported: %s",
metadata.configKey());
result.put(metadata.configKey(), metadata);
}
return new ArrayList<MethodMetadata>(result.values());
}
这里主要是对Feign接口类做了一些校验,比如说不支持泛型,只能单一接口继承等,以及检验接口中的方法是否符合代理规则,比如说是否是继承自Object类的方法、是否是static方法、是否是default方法,如果是的话则不进行解析,如果不是的话则循环解析接口中的各个方法,解析的具体方法是parseAndValidateMetadata(Class<?> targetType, Method method),如下代码段:
protected MethodMetadata parseAndValidateMetadata(Class<?> targetType, Method method) {
MethodMetadata data = new MethodMetadata();
data.returnType(Types.resolve(targetType, targetType, method.getGenericReturnType()));
data.configKey(Feign.configKey(targetType, method));
if(targetType.getInterfaces().length == 1) {
processAnnotationOnClass(data, targetType.getInterfaces()[0]);
}
processAnnotationOnClass(data, targetType);
for (Annotation methodAnnotation : method.getAnnotations()) {
processAnnotationOnMethod(data, methodAnnotation, method);
}
checkState(data.template().method() != null,
"Method %s not annotated with HTTP method type (ex. GET, POST)",
method.getName());
Class<?>[] parameterTypes = method.getParameterTypes();
Type[] genericParameterTypes = method.getGenericParameterTypes();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
int count = parameterAnnotations.length;
for (int i = 0; i < count; i++) {
boolean isHttpAnnotation = false;
if (parameterAnnotations[i] != null) {
isHttpAnnotation = processAnnotationsOnParameter(data, parameterAnnotations[i], i);
}
if (parameterTypes[i] == URI.class) {
data.urlIndex(i);
} else if (!isHttpAnnotation) {
checkState(data.formParams().isEmpty(),
"Body parameters cannot be used with form parameters.");
checkState(data.bodyIndex() == null, "Method has too many Body parameters: %s", method);
data.bodyIndex(i);
data.bodyType(Types.resolve(targetType, targetType, genericParameterTypes[i]));
}
}
if (data.headerMapIndex() != null) {
checkMapString("HeaderMap", parameterTypes[data.headerMapIndex()], genericParameterTypes[data.headerMapIndex()]);
}
if (data.queryMapIndex() != null) {
checkMapString("QueryMap", parameterTypes[data.queryMapIndex()], genericParameterTypes[data.queryMapIndex()]);
}
return data;
}
分析如下:
- 1.获取方法返回类型以及路由key
- 2.处理此接口以及继承的接口上的注解
- 3.处理接口中各个方法上的注解
- 4.处理方法各个参数上的注解
- 5.校验HeaderMap与QueryMap
Feign继承BaseContract的默认子类Default
Feign build的时候如果没有自定义Contract实现类,那将默认使用Default作为接口方法解析的Contract,这个类主要实现了接口注解的处理、方法上注解的处理以及方法参数上注解的处理
接口注解的处理:processAnnotationOnClass
@Override
protected void processAnnotationOnClass(MethodMetadata data, Class<?> targetType) {
if (targetType.isAnnotationPresent(Headers.class)) {
String[] headersOnType = targetType.getAnnotation(Headers.class).value();
checkState(headersOnType.length > 0, "Headers annotation was empty on type %s.",
targetType.getName());
Map<String, Collection<String>> headers = toMap(headersOnType);
headers.putAll(data.template().headers());
data.template().headers(null); // to clear
data.template().headers(headers);
}
}
这个方法主要处理Headers这个注解,如果类上存在Headers注解的话,获取到注解设置的headers,然后将其加入RequestTemplate对象的headers集合中
方法注解的处理:processAnnotationOnMethod
@Override
protected void processAnnotationOnMethod(MethodMetadata data, Annotation methodAnnotation,
Method method) {
Class<? extends Annotation> annotationType = methodAnnotation.annotationType();
if (annotationType == RequestLine.class) {
String requestLine = RequestLine.class.cast(methodAnnotation).value();
checkState(emptyToNull(requestLine) != null,
"RequestLine annotation was empty on method %s.", method.getName());
if (requestLine.indexOf(' ') == -1) {
checkState(requestLine.indexOf('/') == -1,
"RequestLine annotation didn't start with an HTTP verb on method %s.",
method.getName());
data.template().method(requestLine);
return;
}
data.template().method(requestLine.substring(0, requestLine.indexOf(' ')));
if (requestLine.indexOf(' ') == requestLine.lastIndexOf(' ')) {
// no HTTP version is ok
data.template().append(requestLine.substring(requestLine.indexOf(' ') + 1));
} else {
// skip HTTP version
data.template().append(
requestLine.substring(requestLine.indexOf(' ') + 1, requestLine.lastIndexOf(' ')));
}
data.template().decodeSlash(RequestLine.class.cast(methodAnnotation).decodeSlash());
} else if (annotationType == Body.class) {
String body = Body.class.cast(methodAnnotation).value();
checkState(emptyToNull(body) != null, "Body annotation was empty on method %s.",
method.getName());
if (body.indexOf('{') == -1) {
data.template().body(body);
} else {
data.template().bodyTemplate(body);
}
} else if (annotationType == Headers.class) {
String[] headersOnMethod = Headers.class.cast(methodAnnotation).value();
checkState(headersOnMethod.length > 0, "Headers annotation was empty on method %s.",
method.getName());
data.template().headers(toMap(headersOnMethod));
}
}
方法分析如下:
- 1.如果注解是RequestLine类型,则解析注解设置的value,重点在于’ '空格字符串的解析,空格之前的会设置为RequestTemplate的method,空格之后的会解析成追加到url中并进行query的解析,至于具体的query解析后面讲到RequestTemplate的时候再进行赘述
- 2.如果注解是Body类型,解析注解设置的value,判断是否存在占位符’{’,如果不存在则直接作为body,如果存在则需要解析参数
- 3.如果注解是Headers类型,获取到注解设置的headers,然后将其加入RequestTemplate对象的headers集合中
参数上注解的处理:processAnnotationsOnParameter
@Override
protected boolean processAnnotationsOnParameter(MethodMetadata data, Annotation[] annotations,
int paramIndex) {
boolean isHttpAnnotation = false;
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType == Param.class) {
Param paramAnnotation = (Param) annotation;
String name = paramAnnotation.value();
checkState(emptyToNull(name) != null, "Param annotation was empty on param %s.", paramIndex);
nameParam(data, name, paramIndex);
Class<? extends Param.Expander> expander = paramAnnotation.expander();
if (expander != Param.ToStringExpander.class) {
data.indexToExpanderClass().put(paramIndex, expander);
}
data.indexToEncoded().put(paramIndex, paramAnnotation.encoded());
isHttpAnnotation = true;
String varName = '{' + name + '}';
if (!data.template().url().contains(varName) &&
!searchMapValuesContainsSubstring(data.template().queries(), varName) &&
!searchMapValuesContainsSubstring(data.template().headers(), varName)) {
data.formParams().add(name);
}
} else if (annotationType == QueryMap.class) {
checkState(data.queryMapIndex() == null, "QueryMap annotation was present on multiple parameters.");
data.queryMapIndex(paramIndex);
data.queryMapEncoded(QueryMap.class.cast(annotation).encoded());
isHttpAnnotation = true;
} else if (annotationType == HeaderMap.class) {
checkState(data.headerMapIndex() == null, "HeaderMap annotation was present on multiple parameters.");
data.headerMapIndex(paramIndex);
isHttpAnnotation = true;
}
}
return isHttpAnnotation;
}
分析如下:
- 1.遍历参数的各个注解,如果注解是Param类型,将参数索引存入MethodMetadata实例中,并且判断url中、url后面跟着的queries参数、被@Headers注解声明的headers这三者是否包含 {参数名} 字符串如果不存在则加入formParams集合中,代表表单数据
- 2.如果注解是QueryMap类型,将QueryMap类型的Map参数索引以及是否encode的标注存入MethodMetadata实例中
- 3.如果注解是HeaderMap类型,将HeaderMap类型的Map参数索引存入MethodMetadata实例中
- 4.处理参数注解的地方有个小细节,这个方法有一个返回值isHttpAnnotation,如果参数是URI类型的话,会设置一个urlIndex记录以便在创建RequestTemplate的时候更新url,如果参数不是URI类型并且返回值isHttpAnnotation为false的话这个参数将会被当成body类型的