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

how can I solve zero length string exception in 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());
//firstly add IntConverter Class;
public class IntConverter implements Converter {
@Override
public boolean canConvert(Class clazz) {
    return clazz.equals(Integer.class);
}

@Override
public void marshal(Object Obj, HierarchicalStreamWriter arg1, MarshallingContext arg2) {

}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
    String value = reader.getValue();
    if(StringUtils.isEmpty(value)){
        return null;
    }
    return Integer.valueOf(value);
}
}
//secondly , register IntConverter when xstream convert String;
xstream.registerConverter((Converter) new IntConverter());