java - 如何解决 XStream 中的零长度字符串异常

标签 java xstream

这是我的代码:

@XStreamAlias("transaction")
public class Student {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
   }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }

  public static void main(String[] args) {

    String xml1 = "<transaction><name>xiaoming</name><age>20</age>    </transaction>" ; 

    XStream xstream = new XStream(new DomDriver());
    xstream.processAnnotations(Student.class);
    Student stu1 = (Student)xstream.fromXML(xml1);

    System.out.println(stu1);

    String xml2 = "<transaction><name>xiaoming</name><age/></transaction>" ; 

    xstream.processAnnotations(Student.class);
    Student stu2 = (Student)xstream.fromXML(xml2);

    System.out.println(stu2);
}

}

我可以将 xml1 转换为 Stu1,但是当我将 xml2 转换为 Stu2 时,我收到如下错误消息:

Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Zero length string : Zero length string
---- Debugging information ----
message             : Zero length string
cause-exception     : java.lang.NumberFormatException
cause-message       : Zero length string
class               : java.lang.Integer
required-type       : java.lang.Integer
converter-type      : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
wrapped-converter   : com.thoughtworks.xstream.converters.basic.IntConverter
path                : /transaction/age
class[1]            : Student
converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
version             : 1.4.7
-------------------------------
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:474)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:406)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:257)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1185)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1169)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1040)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1031)
    at TestJsonConvert.main(TestJsonConvert.java:20)
Caused by: java.lang.NumberFormatException: Zero length string
    at java.lang.Long.decode(Long.java:893)
    at com.thoughtworks.xstream.converters.basic.IntConverter.fromString(IntConverter.java:27)
    at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.fromString(SingleValueConverterWrapper.java:41)
    at com.thoughtworks.xstream.converters.SingleValueConverterWrapper.unmarshal(SingleValueConverterWrapper.java:49)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    ... 16 more

最佳答案

您需要编写自己的转换器来处理空标签:

public class IntConverter extends AbstractBasicConverter {

  public boolean canConvert(Class type) {    
    return type.equals(int.class) || type.equals(Integer.class);    
  }

  protected Object fromString(String str) {
    /* If empty tag. */    
    if (str == null || str.trim().length == 0) {

      /* Default to zero. */    
      str = "0";
    }

    return Integer.decode(str);
  }
}

按如下方式注册转换器:

xstream.registerConverter(new IntConverter());

关于java - 如何解决 XStream 中的零长度字符串异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34449960/

相关文章:

java - 在 xstream 中反序列化旧版 XML 结构

java - 如何使用 XStream 为 java.lang.Integer 别名?

java - 对象动画只会为对象设置一次动画

java - 将未确认的消息(异常)放入 RabbitMQ 中的不同队列

java - 如何使用 xStream 输出具有列表属性的 Java 对象?

xstream - 在 Kettle 下使用 xstream 时,nodecom.thoughtworks.xstream.mapper.CannotResolveClassException

java - 是否可以在注释中使用 NamedMapConverter?如何?

java - Perl SHA-256 算法生成的输出与等效的 Java 不同

java - 赋值是通过引用?

Java - emoji4j 静态方法调用结束/消失/死亡,没有错误