正如人们在评论中所说的,您可以轻松地编写您的注释驱动的自定义解析器.四个简单的步骤,
>创建一个注释,例如
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UpperCase {
String value();
}
>写一个解析器,例如
public class UpperCaseResolver implements HandlerMethodArgumentResolver {
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(UpperCase.class) != null;
}
public Object resolveArgument(MethodParameter parameter,ModelAndViewContainer mavContainer,NativeWebRequest webRequest,WebDataBinderFactory binderFactory) throws Exception {
UpperCase attr = parameter.getParameterAnnotation(UpperCase.class);
return webRequest.getParameter(attr.value()).toUpperCase();
}
}
>注册解析器
或java配置
@Configuration
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter {
...
@Override
public void addArgumentResolvers(List argumentResolvers) {
argumentResolvers.add(new UpperCaseResolver());
}
...
}
>在控制器方法中使用注释,例如
public String test(@UpperCase("foo") String foo)