JAXB解组无效整数

时间:2012-11-04 06:40:43

标签: jaxb cxf unmarshalling

在我们的产品中,我们使用apache CXF。由于性能限制,架构验证已设置为false。现在,对于整数元素,如果我提供的值无效,JAXB会将其解组为其他内容。例如,

9999999999转换为141006540​​7。

988888888888转换为1046410808。

我的问题是这里遵循的逻辑(公式)是什么?

如何处理(考虑验证将关闭)?我想要拒绝这样的价值。

1 个答案:

答案 0 :(得分:7)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

简短回答

这似乎是JAXB参考实现中的错误。我建议在以下位置输入错误:

这个用例在EclipseLink JAXB(MOXy)中正常工作。


LONG ANSWER

以下是演示此问题的完整示例:

以下是包含intinteger字段的域类。

package forum13216624;

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    int int1;
    int int2;
    Integer integer1;
    Integer integer2;

}

input.xml中

下面是一个XML文档,其中包含您问题的值。

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <int1>9999999999</int1>
    <int2>988888888888</int2>
    <integer1>9999999999</integer1>
    <integer2>988888888888</integer2>
</root>

演示

在下面的演示代码中,我在ValidationEventHandler上指定了Unmarshaller。对于在ValidationEvent操作期间遇到的任何无效值,这应该捕获unmarhsal

package forum13216624;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                System.out.println(event.getMessage());
                return true;
            }}

        );
        File xml = new File("src/forum13216624/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        System.out.println(root.int1);
        System.out.println(root.int2);
        System.out.println(root.integer1);
        System.out.println(root.integer2);
    }

}

输出 - JAXB参考实施

此输出与您看到的行为相符。

1410065407
1046410808
1410065407
1046410808

输出 - EclipseLink JAXB(MOXy)

如果将MOXy指定为JAXB提供程序(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html),则此用例将按预期工作。对于每个无效值,您将获得ValidationEvent,如果处理ValidationEvent,则字段/属性将不会设置为无效值。

Exception Description: The object [9999999999], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[int1-->int1/text()]] with descriptor [XMLDescriptor(forum13216624.Root --> [DatabaseTable(root)])], could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "9999999999"

Exception Description: The object [988888888888], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[int2-->int2/text()]] with descriptor [XMLDescriptor(forum13216624.Root --> [DatabaseTable(root)])], could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "988888888888"

Exception Description: The object [9999999999], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[integer1-->integer1/text()]] with descriptor [XMLDescriptor(forum13216624.Root --> [DatabaseTable(root)])], could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "9999999999"

Exception Description: The object [988888888888], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[integer2-->integer2/text()]] with descriptor [XMLDescriptor(forum13216624.Root --> [DatabaseTable(root)])], could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "988888888888"
0
0
null
null

可能的解决方法

如果您的字段/属性属于Integer类型,并且您可以从JAXB参考实现切换,则可以创建XmlAdapter来自Integer来自String转换。

IntegerAdapter

以下是XmlAdapter演示如何提供自己的转换逻辑的示例。

package forum13216624;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class IntegerAdapter extends XmlAdapter<String, Integer>{

    @Override
    public Integer unmarshal(String string) throws Exception {
        return Integer.valueOf(string);
    }

    @Override
    public String marshal(Integer integer) throws Exception {
        return String.valueOf(integer);
    }

}

包信息

在包级别使用@XmlJavaTypeAdapter注释意味着XmlAdapter将适用于此包中类的所有类型Integer的字段/属性。

@XmlJavaTypeAdapter(value=IntegerAdapter.class, type=Integer.class)
package forum13216624;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

输出

以下是使用RI时的更新输出。 int值仍然有误,但Integer值不是nullValidationEvents按预期生成。

java.lang.NumberFormatException: For input string: "9999999999"
java.lang.NumberFormatException: For input string: "988888888888"
1410065407
1046410808
null
null

了解更多信息