如何在springMVC的RequestMappingHandlerMapping中覆盖getMappingForMethod方法

时间:2016-08-07 08:02:04

标签: java spring rest spring-mvc

  1. 首先,我对RequestMappingHandlerMapping执行一次覆盖,如下所示

    public class RestAnnotationHandler extends RequestMappingHandlerMapping{
         Logger logger = LoggerFactory.getLogger(getClass());
    
    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
          RequestMappingInfo info = null;
    
    List<RequestMethod> requestMethod = new ArrayList<>(RequestMethod.values().length);
    String[] value = null;
    if(method.isAnnotationPresent(Post.class)){
        Post postAnnotation = AnnotationUtils.findAnnotation(method, Post.class);
        requestMethod.add(RequestMethod.POST);
        value = postAnnotation.value();
    }else if(method.isAnnotationPresent(Get.class)){
        Get getAnnotation = AnnotationUtils.findAnnotation(method, Get.class);
        requestMethod.add(RequestMethod.GET);
        value = getAnnotation.value();
    }else if(method.isAnnotationPresent(Put.class)){
        Put putAnnotation = AnnotationUtils.findAnnotation(method, Put.class);
        requestMethod.add(RequestMethod.PUT);
        value = putAnnotation.value();
    }else if(method.isAnnotationPresent(Delete.class)){
        Delete deleteAnnotation = AnnotationUtils.findAnnotation(method, Delete.class);
        requestMethod.add(RequestMethod.DELETE);
        value = deleteAnnotation.value();
    }else if(method.isAnnotationPresent(RequestMapping.class)){
        RequestMapping annotation = AnnotationUtils.findAnnotation(method, RequestMapping.class);
        requestMethod.addAll(Arrays.asList((annotation.method()!=null && annotation.method().length>0)?annotation.method():RequestMethod.values()));
        value = annotation.value();
    }
    
    final String[] requestMappingValue = value;
    if(requestMethod.size() > 0){
        final RequestMethod[] requestMappingRequestMethod = requestMethod.toArray(new RequestMethod[requestMethod.size()]);
        logger.info("Open a web rest annotation: {} and method {}!", requestMappingValue, requestMappingRequestMethod.toString());
        RequestMapping methodAnnotation  = new RequestMapping() {
            @Override
            public Class<? extends Annotation> annotationType() {
                return RequestMapping.class;
            }
    
            @Override
            public String name() {
                return "";
            }
    
            @Override
            public String[] value() {
                return requestMappingValue;
            }
    
            @Override
            public String[] produces() {
                return new String[]{};
            }
    
            @Override
            public String[] params() {
                return new String[]{};
            }
    
            @Override
            public RequestMethod[] method() {
                return requestMappingRequestMethod;
            }
    
            @Override
            public String[] headers() {
                return new String[]{};
            }
    
            @Override
            public String[] consumes() {
                return new String[]{};
            }
    
            @Override
            public String[] path() {
                return requestMappingValue;
            }
        };
    
        RequestCondition<?> methodCondition = getCustomMethodCondition(method);
        info = createRequestMappingInfo(methodAnnotation, methodCondition);
        RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
        if (typeAnnotation != null) {
            RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
            info = createRequestMappingInfo(typeAnnotation, typeCondition).combine(info);
         }
       }
      return info;
    }
    
    }
    
  2. 然后我在org.springframework.web.context.ContextLoaderListener加载的spring context.xml中配置它,所以这个bean将在root WebApplicationContext中。

    <bean id="RequestMappingHandlerMapping" class="com.hisoka.handler.RestAnnotationHandler"/>
    
  3. 现在我们可以使用my rest Annotation来注释一个控制方法,我可以在浏览器上访问该方法。

    @Documented
    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Get {
    
       String[] value() default{};
    
    }
    
    @Get("/")
    @ResponseBody
    public ModelAndView index() {
       Map<String, Object> result = new HashMap<String, Object>();
       result.put("name", "Hinsteny");
       return new ModelAndView("home").addObject("name", "Hinsteny Hisoka");
    }
    
  4. 但是当我配置DefaultServletHttpRequestHandler处理静态资源时,我自己休息Annotation无法正常工作

    <mvc:default-servlet-handler/>
    
  5. 问题:如何使我的休息Annotation和DefaultServletHttpRequestHandler在springmv中一起工作?请帮帮我

0 个答案:

没有答案