spring MVC 自定义注解实现路径匹配

时间:2024-03-18 17:22:26

1.controller 类型没有 @RequestMapping 注解


//首先自定义注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomRequestMapping {

    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};

    RequestMethod[] method() default {RequestMethod.GET};
}

//自定义逻辑

@Component
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
        if (AnnotationUtils.findAnnotation(method, CustomRequestMapping.class) != null) {

            super.registerHandlerMethod(handler, method, mapping);

        }

    }


@Override
public int getOrder() {
    return -2;
}

    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {

        // 获取自定义注解
        CustomRequestMapping customMapping = AnnotationUtils.findAnnotation(method, CustomRequestMapping.class);
        if (customMapping != null) {
             return    RequestMappingInfo
                    .paths(customMapping.value())
                    .methods(customMapping.method())
                    .params(customMapping.params())
                    .headers(customMapping.headers())
                    .consumes(customMapping.consumes())
                    .produces(customMapping.produces())
                    .mappingName(customMapping.name())
                    .build();
          
        }
        return super.getMappingForMethod(method, handlerType);
    }
}



2.controller 类型有 @RequestMapping 注解

@Component
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
    @Override
    protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
        if (AnnotationUtils.findAnnotation(method, CustomRequestMapping.class) != null) {

            super.registerHandlerMethod(handler, method, mapping);

        }

    }


@Override
public int getOrder() {
    return -2;
}

    @Override
    protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
        // 获取类级别的RequestMapping注解
        RequestMapping classRequestMapping = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class);
        RequestMappingInfo classInfo = null;
        if (classRequestMapping != null) {
            classInfo = createRequestMappingInfo(classRequestMapping, null);
        }

        // 获取自定义注解
        CustomRequestMapping customMapping = AnnotationUtils.findAnnotation(method, CustomRequestMapping.class);
        if (customMapping != null) {
            RequestMappingInfo builder = RequestMappingInfo
                    .paths(customMapping.value())
                    .methods(customMapping.method())
                    .params(customMapping.params())
                    .headers(customMapping.headers())
                    .consumes(customMapping.consumes())
                    .produces(customMapping.produces())
                    .mappingName(customMapping.name())
                    .build();
            return classInfo == null ? builder : classInfo.combine(builder);
        }
        return super.getMappingForMethod(method, handlerType);
    }
}