请求参数的自定义Spring注释 - 从不调用自定义解析器

时间:2018-02-26 14:53:40

标签: spring-mvc spring-annotations resolver

我在我的项目中实现了自定义注释,但从未调用实现的方法参数解析器。 有人可以帮我解决这个问题吗?

我的实施:

注释

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    String value();
}

解析器

public class MyAnnotationResolver implements HandlerMethodArgumentResolver {

   public boolean supportsParameter(MethodParameter parameter) {
         System.out.println("supportsParameter invoked!");
         return parameter.getParameterAnnotation(MyAnnotation.class) != null;
   }

   public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
        ...
        return "someString";
    }
}

Configuaration

@Configuration
@EnableWebMvc 
public class Config extends WebMvcConfigurerAdapter { 
  ...   
   @Override   
   public void addArgumentResolvers(List<HandlerMethodArgumentResolver>  argumentResolvers) {
      argumentResolvers.add(new MyAnnotationResolver());
      System.out.println("Resolver added!");   
   } 
... 
}

控制器

@Controller
@RequestMapping("/project")
public class ProjectController{

    @RequestMapping(method = RequestMethod.GET, value= "/{hashCode}")
    @ResponseBody
    public String index(@MyAnnotation("hashCode") String hashCode, Model model) {
    ...
    System.out.println(hashCode == null ? "HashCode=null" : "HashCode=" + hashCode);
}   

作为输出我得到:

Resolver added!

HashCode=null

为什么从未调用 supportsParameter(...)

0 个答案:

没有答案