使用JacksonAnnotationIntrospector.hasIgnoreMarker()

时间:2016-10-17 23:31:53

标签: java jackson

我想只在我的类中包含具有自定义注释@MyInclude的字段,但杰克逊最终忽略了所有内容。我做错了什么?

 public static void main(String[] args) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.setAnnotationIntrospector(new IgnoreIntrospector());

    MyNestedObject nestedObject = new MyNestedObject("value1", "value2");

    MyObject object = new MyObject();
    object.setNestedObject(nestedObject);

    String json = mapper.writeValueAsString(object); //This returns {}
  }

  public static class IgnoreIntrospector extends JacksonAnnotationIntrospector {
    private static final long serialVersionUID = -3951086067314107368L;

    @Override
    public boolean hasIgnoreMarker(AnnotatedMember m) {
      return !m.hasAnnotation(MyInclude.class) || super.hasIgnoreMarker(m);
    }
  }


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyObject {
  @MyInclude
  private MyNestedObject nestedObject;
}

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyNestedObject {
  @MyInclude
  private String value1;
  private String value2;
}

mapper.writeValueAsString(object)正在返回{},但它应该返回NestedObject而填充了value1(忽略value2)。

如果我将IgnoreIntrospector.hasIgnoreMarker ()更新为super.hasIgnoreMarker(m),则所有内容都会包含在json字符串中。

2 个答案:

答案 0 :(得分:1)

IgnoreIntrospector还不够。由于我的自定义注释只在字段上,因此我需要禁用所有可见性:

mapper.setAnnotationIntrospector(new IgnoreIntrospector());

mapper.setVisibility(mapper.getSerializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

mapper.setVisibility(mapper.getDeserializationConfig().getDefaultVisibilityChecker()
.withFieldVisibility(JsonAutoDetect.Visibility.ANY)
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withSetterVisibility(JsonAutoDetect.Visibility.NONE)
.withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

并更新了我的IgnoreIntrospector

 public static class IgnoreIntrospector extends JacksonAnnotationIntrospector {

    @Override
    public boolean hasIgnoreMarker(AnnotatedMember m) {
      return m instanceof AnnotatedField && _findAnnotation(m, MyInclude.class) == null;
    }
  }

答案 1 :(得分:0)

或者,同时覆盖hasIgnoreMarker()findNameForSerialization()

JsonMapper jsonMapper = JsonMapper.builder()
                                  .annotationIntrospector(new JacksonAnnotationIntrospector()
                                  {
                                      @Override
                                      public boolean hasIgnoreMarker(AnnotatedMember m)
                                      {
                                          return m.hasAnnotation(CustomIgnore.class) || super.hasIgnoreMarker(m);
                                      }

                                      @Override
                                      public PropertyName findNameForSerialization(Annotated a)
                                      {
                                          if(a.hasAnnotation(CustomIgnore.class)) return null;
                                          return super.findNameForSerialization(a);
                                      }
                                  })
                                  .build();