简体   繁体   English

如何为实体中的修剪字段制作自定义注释?

[英]How to make a custom annotation for trimmed field in entities?

In Spring Boot and Java 8, have multiple models: DTOs, Entities, ViewModels, etc, but need get and save the description of a person, but the description need trim the value, by example:在 Spring Boot 和 Java 8,有多个模型:DTOs,Entities,ViewModels等,但是需要获取并保存一个人的描述,但是描述需要修剪值,举个例子:

    @Size(min=0, max=1024)
    private String description;

    public String getDescription() {
        return ((this.description != null) ? this.description.trim() : null);
    }

    public void setDescription(String description) {
        this.description = ((description != null) ? description.trim() : null);
    }

I make a custom annotation like as @Trimmed for automatize it like as:我制作了一个自定义注释,如@Trimmed以将其自动化,如下所示:

    @Trimmed
    @Size(min=0, max=1024)
    private String description;

I try make the interface and the aspect:我尝试制作界面和方面:

Trimmed.java修剪.java

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Trimmed {
}

TrimmedAspect.java TrimmedAspect.java

@Aspect
@Component
public class TrimmedAspect {

    @Around("@annotation(Trimmed)")
    public Object requestIntercept ...

}

But, how to make the aspect?但是,如何做纵横呢? in the inte.net can find examples using functions and class declarations but no fields when get and set values.在 inte.net 中可以找到使用函数和 class 声明但在获取和设置值时没有字段的示例。

How to can made this?如何才能做到这一点?

I just made an example custom annotation with AOP, in my component Event responsible for this: I just tried this code maybe it helps: it will be executed after the user is created我刚刚用 AOP 制作了一个示例自定义注释,在我的组件事件中对此负责:我刚刚尝试了这段代码也许它有帮助:它将在创建用户后执行

 @AfterReturning(value = "execution(* ypour.package..*(..))", returning = "retVal")
    public void requestIntercept(JoinPoint pjp, Object retVal) throws JsonProcessingException {

        //----- your implementation here 
            }


        }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM