Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 使用BeanPostProcessor在Spring中进行自定义注释_Spring Boot_Annotations - Fatal编程技术网

Spring boot 使用BeanPostProcessor在Spring中进行自定义注释

Spring boot 使用BeanPostProcessor在Spring中进行自定义注释,spring-boot,annotations,Spring Boot,Annotations,我们正试图在Spring中为RESTAPI创建一个自定义注释。我不熟悉创建自定义注释,我给出了下面的代码片段 Spring启动应用程序-- RestController-- 自定义注释-- 注释处理器-- 方法回调-- 我试图在实现BeanPostProcessor的类中处理自定义注释,但我遇到了一个问题 问题1:回调调用了一次,但我无法对/service/v1/version API的每个请求应用验证。我需要验证每个请求,我们的设计/方法正确吗?如果正确,如何解决这个问题,如果不正确,请建议不

我们正试图在Spring中为RESTAPI创建一个自定义注释。我不熟悉创建自定义注释,我给出了下面的代码片段

Spring启动应用程序--

RestController--

自定义注释--

注释处理器--

方法回调--

我试图在实现BeanPostProcessor的类中处理自定义注释,但我遇到了一个问题


问题1:回调调用了一次,但我无法对/service/v1/version API的每个请求应用验证。我需要验证每个请求,我们的设计/方法正确吗?如果正确,如何解决这个问题,如果不正确,请建议不同的方法

问题2:如果我需要将完整的请求对象(与标题一起)传递给我的@customAnnotation,我应该怎么做

如果您需要更多详细信息,请告诉我


感谢

处理定制注释@validateAuthentication,我们创建了拦截器类扩展HandlerInterceptorAdapter


我们在preHandle(HttpServletRequest请求、HttpServletResponse响应、对象处理程序)中实现对请求头的验证方法。

问题1:回调被调用一次,但我无法对
/service/v1/version
API的每个请求应用验证。我需要验证每个请求,我们的设计/方法正确吗?如果正确,如何解决这个问题,如果不正确,请建议不同的方法

问题2:如果我需要将完整的请求对象(与标题一起)传递给我的
@customAnnotation
,我应该如何做

我认为您应该使用SpringAOP或Interceptor,而不是Annotation

@SpringBootApplication
@ComponentScan(basePackageClasses = {ServiceController.class, CustomAnnotatorProcessor.class})
public class ServiceApp {
    public static void main(String[] args) {            
        SpringApplication.run(ServiceApp.class, args);
    }
}
@RestController
public class ServiceController {

    @RequestMapping(method = RequestMethod.GET, value="/service/v1/version")
    @ApiResponses(value = { 
            @ApiResponse(code = 200, message = "Success", response = String.class),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 404, message = "Not Found"),
            @ApiResponse(code = 500, message = "Failure")}) 
    @CustomAnnotation()
    public String getVersion() {
        return "success";
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface CustomAnnotation {

}
@Component
public class CustomAnnotatorProcessor implements BeanPostProcessor {    

private ConfigurableListableBeanFactory configurableBeanFactory;

@Autowired
public CustomAnnotatorProcessor(ConfigurableListableBeanFactory beanFactory) {
    this.configurableBeanFactory = beanFactory;
}

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

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    MethodCallback methodCallback = new CustomAnnotationMethodCallback(configurableBeanFactory, bean);
    ReflectionUtils.doWithMethods(bean.getClass(), methodCallback);
    return bean;
}
public class CustomAnnotationMethodCallback implements MethodCallback{
    @Override
    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
        if (method.isAnnotationPresent(CustomAnnotation.class)) {
            System.out.println("doWith is getting called for CustomAnnotationMethodCallback");
            ReflectionUtils.makeAccessible(method);     
            //DO VALIDATION WHETHER A SPECIFIC HEADER IS PRESENT IN THE GIVEN REQUEST
            return;
        }       
    }

}