【问题标题】:Mapping annotations with Jackson使用 Jackson 映射注释
【发布时间】:2014-06-02 06:48:39
【问题描述】:

我正在尝试使用 Jackson 库将一些注释映射到休息响应

@Retention( RetentionPolicy.RUNTIME )
@Target( ElementType.TYPE )
public @interface LogicModule
{
    String id();
    int numberOfInputs();
}

我将这些对象转换为 JSON 的代码是

...
final List<LogicModule> result = new LinkedList<>();
for (final Class<?> annotatedClass : annotated)
{
   final LogicModule moduleInfo = annotatedClass.getAnnotation( LogicModule.class );
   result.add(moduleInfo);
}
return Response.ok( result ).build();

不幸的是,这会返回一个空列表。我需要如何更改代码才能使事情按预期工作?

【问题讨论】:

  • result 是否包含任何内容?
  • 是的,它包含LogicModule的几个实例

标签: java json serialization jackson


【解决方案1】:

我认为您有两个选择:要么用@JsonProperty 注释标记注释类型的方法,要么配置自定义AnnotationIntrospector,它将所有注释类型方法视为普通属性。

这是一个显示两者的示例:

public class JacksonAnnotation {

    @Retention( RetentionPolicy.RUNTIME )
    @Target( ElementType.TYPE )
    public static @interface LogicModule {
        @JsonProperty
        String id();
        @JsonProperty
        int numberOfInputs();
    }

    @Retention( RetentionPolicy.RUNTIME )
    @Target( ElementType.TYPE )
    public static @interface LogicModule2 {
        String id();
        int numberOfInputs();
    }

    @LogicModule(id = "id", numberOfInputs = 123)
    public static class AnnotatedClass {

    }
    @LogicModule2(id = "id2", numberOfInputs = 321)
    public static class AnnotatedClass2 {

    }

    public static void main(String[] args) throws JsonProcessingException {
        List<LogicModule> modules = singletonList(AnnotatedClass.class.getAnnotation(LogicModule.class));
        List<LogicModule2> modules2 = singletonList(AnnotatedClass2.class.getAnnotation(LogicModule2.class));

        ObjectMapper mapper1 = new ObjectMapper();
        System.out.println(mapper1.writerWithDefaultPrettyPrinter().writeValueAsString(modules));

        ObjectMapper mapper2 = new ObjectMapper();
        mapper2.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
            @Override
            public PropertyName findNameForSerialization(Annotated a) {
                if (a instanceof AnnotatedMethod) {
                    Class<?> declaringClass = ((AnnotatedMethod) a).getMember().getDeclaringClass();
                    // checks that this is an annotation method
                    if (Annotation.class.isAssignableFrom(declaringClass)) {
                        try {
                            // exclude the other method defined in the super interface
                            Annotation.class.getMethod(a.getName());
                        } catch (NoSuchMethodException e) {
                            return new PropertyName(a.getName());
                        }
                    }
                }
                return super.findNameForSerialization(a);
            }
        });
        System.out.println(mapper2.writerWithDefaultPrettyPrinter().writeValueAsString(modules2));
    }
}

输出:

[ {
  "id" : "id",
  "numberOfInputs" : 123
} ]
[ {
  "id" : "id2",
  "numberOfInputs" : 321
} ]

【讨论】:

    猜你喜欢
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2018-01-23
    • 2023-03-22
    相关资源
    最近更新 更多