参数分析器 MethodArgumentResolver
- v3.4.1 到 v3.6.0 之间:
名为 ActionArgumentResolver(v3.6.1 之后,标为弃用)
- v3.6.1 之后
名为 MethodArgumentResolver
前置参考:
v3.4.1 后支持。为 Action (即 @Mapping 注解的方法)参数注入提供单个参数分析支持。像下面这个示例,可以通过类型特征或注解特性进行分析定制(一般不需要):
@Controller
public class DemoController {
@Mapping("case1")
public void case1(User user){
}
@Mapping("case2")
public void case2(@Anno User user){
}
}
1、接口声明
package org.noear.solon.core.handle;
import org.noear.solon.core.util.LazyReference;
import org.noear.solon.core.wrap.MethodWrap;
import org.noear.solon.core.wrap.ParamWrap;
public interface MethodArgumentResolver {
/**
* 是否匹配
*
* @param ctx 请求上下文
* @param pWrap 参数包装器
*/
boolean matched(Context ctx, ParamWrap pWrap);
/**
* 参数分析
*
* @param ctx 请求上下文
* @param target 控制器
* @param mWrap 函数包装器
* @param pWrap 参数包装器
* @param pIndex 参数序位
* @param bodyRef 主体引用
*/
Object resolveArgument(Context ctx, Object target, MethodWrap mWrap, ParamWrap pWrap, int pIndex, LazyReference bodyRef) throws Throwable;
}
2、定制参考1
@Component
public class MethodArgumentResolverImpl implements MethodArgumentResolver {
@Override
public boolean matched(Context ctx, ParamWrap pWrap) {
return pWrap.getAnnotation(Argument.class) != null;
}
@Override
public Object resolveArgument(Context ctx, Object target, MethodWrap mWrap, ParamWrap pWrap, int pIndex, LazyReference bodyRef) throws Throwable {
Argument anno = pWrap.getParameter().getAnnotation(Argument.class);
if (User.class.equals(pWrap.getType())) {
return new User();
}
return null;
}
/**
* <pre>{@code
* @Controller
* public class DemoController {
* @Mapping("/hello")
* public User hello(@Argument User user) {
* return user;
* }
* }
* }</pre>
*/
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Argument {
String value() default "";
}
public class User {
}
}