使用自定义注释扩展类级别的RequestMapping

时间:2018-08-17 14:12:41

标签: java spring request-mapping java-annotations

我想在我的Spring Boot应用程序中创建一个自定义批注,该批注始终向类级别的RequestMapping path 添加前缀。 / p>

我的控制器:

import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;

@MyApi("/users")
public class UserController {

    @GetMapping("/stackoverflow")
    public String get() {
        return "Best users";
    }

}

我的自定义注释

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RestController
@RequestMapping(path = "/api")
public @interface MyApi {

    @AliasFor(annotation = RequestMapping.class)
    String value();

}

目标:最后这样的映射:/api/users/stackoverflow

注意:

  • server.servlet.context-path不是一个选项,因为我想创建 其中几个
  • 我正在使用Spring Boot版本2.0.4

1 个答案:

答案 0 :(得分:1)

我无法找到解决该问题的理想方法。但是,这可行:

略微修改了注释,因为事实证明更改value的行为更加困难。

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RestController
@RequestMapping
public @interface MyApi {

    @AliasFor(annotation = RequestMapping.class, attribute = "path")
    String apiPath();

}

Bean注释处理器

import com.sagemcom.smartvillage.smartvision.common.MyApi;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

@Component
public class MyApiProcessor implements BeanPostProcessor {

    private static final String ANNOTATIONS = "annotations";
    private static final String ANNOTATION_DATA = "annotationData";

    public Object postProcessBeforeInitialization(@NonNull final Object bean, String beanName) throws BeansException {
        MyApi myApi = bean.getClass().getAnnotation(MyApi.class);
        if (myApi != null) {
            MyApi alteredMyApi = new MyApi() {

                @Override
                public Class<? extends Annotation> annotationType() {
                    return MyApi.class;
                }

                @Override
                public String apiPath() {
                    return "/api" + myApi.apiPath();
                }

            };
            alterAnnotationOn(bean.getClass(), MyApi.class, alteredMyApi);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(@NonNull Object bean, String beanName) throws BeansException {
        return bean;
    }

    @SuppressWarnings("unchecked")
    private static void alterAnnotationOn(Class clazzToLookFor, Class<? extends Annotation> annotationToAlter, Annotation annotationValue) {
        try {
            // In JDK8 Class has a private method called annotationData().
            // We first need to invoke it to obtain a reference to AnnotationData class which is a private class
            Method method = Class.class.getDeclaredMethod(ANNOTATION_DATA, null);
            method.setAccessible(true);
            // Since AnnotationData is a private class we cannot create a direct reference to it. We will have to manage with just Object
            Object annotationData = method.invoke(clazzToLookFor);
            // We now look for the map called "annotations" within AnnotationData object.
            Field annotations = annotationData.getClass().getDeclaredField(ANNOTATIONS);
            annotations.setAccessible(true);
            Map<Class<? extends Annotation>, Annotation> map = (Map<Class<? extends Annotation>, Annotation>) annotations.get(annotationData);
            map.put(annotationToAlter, annotationValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

控制器:

import com.sagemcom.smartvillage.smartvision.common.MyApi;
import org.springframework.web.bind.annotation.GetMapping;

@MyApi(apiPath = "/users")
public class UserController {

    @GetMapping("/stackoverflow")
    public String get() {
        return "Best users";
    }

}