0

嗨,我在使用带有 ObjectMapper 类的 Jackson JSON PRocessor 时遇到了一个令人讨厌的问题。这是我的测试类,应该将对象(UserHeader)序列化为 Json 字符串。

 import org.codehaus.jackson.map.ObjectMapper;
 import org.codehaus.jackson.map.SerializationConfig;

 public class TestJson {

   public static void main(String[] args) throws Exception {
            ObjectMapper mapper = new ObjectMapper();
            UserHeader userHeader = new UserHeader();
            userHeader.setFirstName("A");
            userHeader.setLastName("A1");
            userHeader.setSystem("2A");

            mapper.configure(SerializationConfig.Feature.USE_ANNOTATIONS, false);
            StringWriter sw = new StringWriter();
            mapper.writeValue(sw, userHeader);
            System.out.println(sw.toString());
    }
 } 

这是我的 UserHeader 类,带有从不同的 ObjectMapper 使用的注释(不是这个)

  import org.codehaus.jackson.annotate.JsonIgnore;
  import org.codehaus.jackson.annotate.JsonProperty;
  import org.codehaus.jackson.map.annotate.JsonSerialize;
  import org.codehaus.jackson.map.annotate.JsonSerialize.Typing;

  @JsonSerialize(typing=Typing.STATIC)
  public class UserHeader implements Serializable,LoggedObject, MessagesInterface {

   private static final long serialVersionUID = 1L;

   private String system;
   private String userName;
   private String firstName;
   private String lastName;

       private List<Scrivania> scrivanie;

   public String getUserName() {
      return userName;
   }

   public void setUserName(String userName) {
    this.userName = userName;
   }

       @JsonProperty("Nome utente")
   public String getFirstName() {
    return firstName;
   }

   public void setFirstName(String firstName) {
    this.firstName = firstName;
   }

   @JsonProperty("Cognome utente")
   public String getLastName() {
    return lastName;
   }
   public void setLastName(String lastName) {
    this.lastName = lastName;
   }

      @JsonProperty("Scrivanie associate")
  public List<Scrivania> getScrivanie() {
    return scrivanie;
   }
   public void setScrivanie(List<Scrivania> scrivanie) {
    this.scrivanie = scrivanie;
   }
   @JsonProperty("Sistema (IAM o EDOC)")
   public String getSystem() {
    return system;
   }
   public void setSystem(String system) {
    this.system = system;
   }

   @Override
   @JsonIgnore
    public String getObjectId() {
    return this.userName;
   }

   @Override
   @JsonIgnore
   public Object getObjectData() {
    try {
        return this.clone();
    }
    catch (Exception e) {
        return null;
    }
   }
   @Override
   public String getName() {
    return this.userName;
    }
    }

但是,如果我运行 main 方法,系统会返回给我这个异常:

Exception in thread "main" java.lang.NullPointerException
at org.codehaus.jackson.map.introspect.AnnotatedClass.resolveClassAnnotations(AnnotatedClass.java:295)
        at org.codehaus.jackson.map.introspect.AnnotatedClass.construct(AnnotatedClass.java:141)
        at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forClassAnnotations(BasicClassIntrospector.java:185)
        at org.codehaus.jackson.map.introspect.BasicClassIntrospector.forClassAnnotations(BasicClassIntrospector.java:15)
        at org.codehaus.jackson.map.SerializationConfig.introspectClassAnnotations(SerializationConfig.java:661)
        at org.codehaus.jackson.map.ser.BasicSerializerFactory.createTypeSerializer(BasicSerializerFactory.java:180)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory.findPropertyContentTypeSerializer(BeanSerializerFactory.java:406)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory._constructWriter(BeanSerializerFactory.java:778)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory.findBeanProperties(BeanSerializerFactory.java:608)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory.constructBeanSerializer(BeanSerializerFactory.java:436)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory.findBeanSerializer(BeanSerializerFactory.java:349)
        at org.codehaus.jackson.map.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:295)
        at org.codehaus.jackson.map.ser.StdSerializerProvider._createUntypedSerializer(StdSerializerProvider.java:778)
        at org.codehaus.jackson.map.ser.StdSerializerProvider._createAndCacheUntypedSerializer(StdSerializerProvider.java:731)
        at org.codehaus.jackson.map.ser.StdSerializerProvider.findValueSerializer(StdSerializerProvider.java:369)
        at http://stackoverflow.com/questions/6661717/how-to-process-an-invalid-value-with-jackson-json-processororg.codehaus.jackson.map.ser.StdSerializerProvider.findTypedValueSerializer(StdSerializerProvider.java:452)
        at org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:597)
        at org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:280)
        at org.codehaus.jackson.map.ObjectMapper._configAndWriteValue(ObjectMapper.java:2260)
        at org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1813)
        at it.unina.edoc.json.TestJson.main(TestJson.java:65)

我不知道这个异常,因为由于 USE_ANNOTATION 错误配置,@Annotations 应该被忽略。此外,如果我将 USE_ANNOTATION 设置为 true,错误就会消失。我的构建路径上有这些罐子:

  • 杰克逊核心asl-1.8.3.jar
  • 杰克逊-jaxrs-1.8.3.jar
  • jackson-mapper-asl-1.8.3.jar
  • 杰克逊-xc-1.8.3.jar
4

1 回答 1

3

DeserializationConfig.Feature.USE_ANNOTATIONS 属性(设置为 false)的使用将导致 JACKSON DeserializerConfig 类使用 NopAnnotationIntrospector。然后将使用此 NopAnnotationIntrospector 解析类的注释。NopAnnotationIntrospector 将对类上的任何注释的每个 isHandled 请求返回 false - 实际上不会在进一步处理中使用此注释。

所以 - 系统仍然“检查”注释 - 在这种情况下必须在 Classpath 上。由于 Android 不提供任何 jaxb-api 注释,这会导致 NoClassDefFoundError。

我预计 USE_ANNOTATIONS = false 会让 JACKSON 完全忽略任何注释 - 但不幸的是它没有。我现在将使用 Jackson Streaming API 来解析 JSON 字符串,而不是使用 JACKSON 数据绑定功能。

于 2012-05-02T17:07:44.497 回答