一个很好的问题。如果您在批注类型方法中添加@JsonProperty
批注,则序列化可以正常工作。这里有一个例子:
@JacksonAnnotationSerialization.MyAnnotation(a = "abc", b = 123)
public class JacksonAnnotationSerialization {
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
@JsonProperty
String a();
@JsonProperty
int b();
}
static class Thing {
public final String field;
public final MyAnnotation myAnnotation;
Thing(final String field, final MyAnnotation myAnnotation) {
this.field = field;
this.myAnnotation = myAnnotation;
}
}
public static void main(String[] args) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
final MyAnnotation annotation
= JacksonAnnotationSerialization.class.getAnnotation(MyAnnotation.class);
final Thing thing = new Thing("value", annotation);
System.out.println(objectMapper.writeValueAsString(thing));
}
}
输出:
{"field":"value","myAnnotation":{"a":"abc","b":123}}
你的问题是不是序列化的注释。对序列化感兴趣但不是反序列化不计算。不清楚你要问什么,直到没有任何意义。 – EJP
我的问题正是关于序列化注释。 –