交流
商城
MCN
登入
注册
首页
提问
分享
讨论
建议
公告
动态
发表新帖
发表新帖
Fegin 第5-1 章:AnnotatedParameterProcessor
分享
未结
0
1122
李延
LV6
2021-06-22
悬赏:20积分
# 作用 用于Fegin参数的解析,在Contract中我们看到了它的调用过程,这里我们主要看一下默认的几个是如何绑定数据的。 # AnnotatedParameterProcessor 顶级接口,定义两个接口 - getAnnotationType 用于判断当前对象可以解析的注解 - processArgument解析过程 ## 部分实现类代码 ```java private static final Class<MatrixVariable> ANNOTATION = MatrixVariable.class; @Override public Class<? extends Annotation> getAnnotationType() { return ANNOTATION; } @Override public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { int parameterIndex = context.getParameterIndex(); Class<?> parameterType = method.getParameterTypes()[parameterIndex]; MethodMetadata data = context.getMethodMetadata(); String name = ANNOTATION.cast(annotation).value(); checkState(emptyToNull(name) != null, "MatrixVariable annotation was empty on param %s.", context.getParameterIndex()); context.setParameterName(name); if (Map.class.isAssignableFrom(parameterType)) { data.indexToExpander().put(parameterIndex, this::expandMap); } else { data.indexToExpander().put(parameterIndex, object -> ";" + name + "=" + object.toString()); } return true; } ``` ```java private static final Class<RequestParam> ANNOTATION = RequestParam.class; @Override public Class<? extends Annotation> getAnnotationType() { return ANNOTATION; } @Override public boolean processArgument(AnnotatedParameterContext context, Annotation annotation, Method method) { int parameterIndex = context.getParameterIndex(); Class<?> parameterType = method.getParameterTypes()[parameterIndex]; MethodMetadata data = context.getMethodMetadata(); if (Map.class.isAssignableFrom(parameterType)) { checkState(data.queryMapIndex() == null, "Query map can only be present once."); data.queryMapIndex(parameterIndex); return true; } RequestParam requestParam = ANNOTATION.cast(annotation); String name = requestParam.value(); checkState(emptyToNull(name) != null, "RequestParam.value() was empty on parameter %s", parameterIndex); context.setParameterName(name); Collection<String> query = context.setTemplateParameter(name, data.template().queries().get(name)); data.template().query(name, query); return true; } ``` # 总结 这里我们看到其实就是对于RequestTemplate的各种属性的设置,比如:query、header、indexToExpander等等。我们在使用时说明。
回帖
消灭零回复
提交回复
热议榜
java 相关知识分享
8
好的程序员与不好的程序员
6
写给工程师的十条精进原则
5
spring boot以jar包运行配置的logback日志文件没生成
5
一步一步分析SpringBoot启动源码(一)
5
MockMvc测试
5
【吐槽向】是不是有个吐槽的板块比较好玩
4
logstash jdbc同步mysql多表数据到elasticsearch
3
IntelliJ IDEA 优质License Server
3
.gitignore忽略规则
3
SpringBoot启动源码分析
3
一步一步分析SpringBoot启动源码(三)
3
2
一步一步分析SpringBoot启动源码(二)
2
积分不够将无法发表新帖
2
官方产品
Meta-Boot - 基于MCN
MCN - 快速构建SpringBoot应用
微信扫码关注公众号