【发布时间】:2017-08-04 19:20:47
【问题描述】:
我有一个注解,在它运行时用于方法时,我想将它(9及其所有属性值)转换为 JSON 对象。
注释:
public @interface MyAnnotation {
String name();
Integer age();
}
使用它:
public class MyClass {
@MyAnnotation(name = "test", age = 21)
public String getInfo()
{ ...elided... }
}
当对 MyClass 类型的对象使用反射并从其 getInfo 方法中获取注释时,我希望能够将注释转换为 JSON。但是它没有任何字段(因为@interfaces 不能有字段),那么有没有办法配置一个ObjectMapper 来使用这些方法作为属性呢?
//This just prints {}
new ObjectMapper().writeValueAsString(method.getAnnotation(MyAnnotation.class));
【问题讨论】:
标签: java json annotations jackson objectmapper