Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.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
Java 自定义注释-Spring引导_Java_Spring_Spring Boot_Annotations_Aop - Fatal编程技术网

Java 自定义注释-Spring引导

Java 自定义注释-Spring引导,java,spring,spring-boot,annotations,aop,Java,Spring,Spring Boot,Annotations,Aop,我对Spring Boot中的@注释有一个问题。我需要创建一个自定义注释来检查各种规则并返回TRUE或FALSE 我的自定义批注: @MyAnnotationExample(value="idOfMyRequestHere") @RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" }) @ResponseStatus(Http

我对Spring Boot中的@注释有一个问题。我需要创建一个自定义注释来检查各种规则并返回TRUE或FALSE

我的自定义批注:

@MyAnnotationExample(value="idOfMyRequestHere")
@RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
@ResponseStatus(HttpStatus.OK)
@MyAnnotationExample(value="idOfMyRequestHere")
public Car getTest(@PathVariable("id") Integer id) {

    List<Car> cars = new ArrayList<Car>();

    cars = this.carService.getCars(id);

    return cars;
}
在我的注释中,我需要传递路径{id},是否可能

如果可能的话,在我的注释中,我将使用这个路径{id}来验证一些规则,在验证这些规则之后,返回值将是TRUE或FALSE

如果返回为TRUE,则返回我的汽车,如果为FALSE,则向用户返回错误

我的控制器:

@MyAnnotationExample(value="idOfMyRequestHere")
@RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
@ResponseStatus(HttpStatus.OK)
@MyAnnotationExample(value="idOfMyRequestHere")
public Car getTest(@PathVariable("id") Integer id) {

    List<Car> cars = new ArrayList<Car>();

    cars = this.carService.getCars(id);

    return cars;
}
@RequestMapping(value=“/test/{id}”,method=RequestMethod.GET,products={“application/json;charset=UTF-8”})
@ResponseStatus(HttpStatus.OK)
@MyAnnotationExample(value=“idOfMyRequestHere”)
公共汽车getTest(@PathVariable(“id”)整数id){
List cars=new ArrayList();
cars=this.carService.getCars(id);
返回车辆;
}

那么,您想在注释本身中添加一些验证代码吗?我认为注释用于提供元数据,并将其用于某种验证或业务逻辑可能不是一个好主意….?注释只是提供一些元数据。然后您应该获取元数据并进行任何操作。如何处理注释取决于您需要达到的目标。例如,如果您需要一些JPA验证,那么您可以编写自己的ConstraintValidator。或者您可以编写自己的BeanPostProcessor或HandlerMethodArgumentResolver。有很多变体。但是注释本身并不能做任何事情。你的问题不清楚。请编辑它并描述你想要实现什么,而不是你认为应该如何实现。也许解决问题的方法是在遇到自定义注释时触发一个
@Aspect
,并让它执行您需要执行的任何操作,但也许不是。我只是猜测,但猜测不是答案。因此,请编辑您的问题,以便成为一个,然后这里的好人可以看到自己的建议。