[英]Custom annotation from others annotations in java
I have date in json like:我在 json 中有日期,例如:
{
"date": "04/22/2022 16:01:01"
}
and the class:和 class:
public class Foo{
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss")
private LocalDateTime date;
}
where everything work fine.一切正常。
It is posible to have annotation with @JsonDeserialize
and @JsonFormat
in it?可以在其中使用
@JsonDeserialize
和@JsonFormat
进行注释吗?
I was trying something like this我正在尝试这样的事情
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss")
public @interface MyLocalDateTimeAnnotation{
}
Where class coud look like this: class 看起来像这样:
public class Foo{
@MyLocalDateTimeAnnotation
private LocalDateTime date;
}
But it doesnt work.但它不起作用。
You need to use @JacksonAnnotationsInside你需要使用@JacksonAnnotationsInside
Meta-annotation (annotations used on other annotations) used for indicating that instead of using target annotation (annotation annotated with this annotation), Jackson should use meta-annotations it has.
元注释(用于其他注释的注释)用于指示 Jackson 不应使用目标注释(用此注释注释的注释),而应使用其具有的元注释。 This can be useful in creating "combo-annotations" by having a container annotation, which needs to be annotated with this annotation as well as all annotations it 'contains'.
这在通过具有容器注释来创建“组合注释”时很有用,容器注释需要使用此注释及其“包含”的所有注释进行注释。
Example:例子:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy HH:mm:ss")
@JacksonAnnotationsInside
public @interface MyLocalDateTimeAnnotation {
}
public class Foo{
@MyLocalDateTimeAnnotation
private LocalDateTime date;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.