JAXB XMLAdapter方法不会抛出异常。

时间:2021-10-16 11:41:10

I am using JAXB XMLadapter to marshal and unmarshal Boolean values. The application's XML file will be accessed by C# application also. We have to validate this XML file and this is done using XSD. C# application writes "True" value for Boolean nodes. But the same does get validated by our XSD as it only allows "true/false" or "1/0". So we have kept String for boolean values in XSD and that string will be validated by XMLAdapter to marshal and unmarshal on our side. The XML Adapter is as follows:

我正在使用JAXB XMLadapter对布尔值进行编组和解编组。应用程序的XML文件也将被c#应用程序访问。我们必须验证这个XML文件,这是使用XSD完成的。c#应用程序为布尔节点写入“True”值。但是我们的XSD确实验证了这一点,因为它只允许“true/false”或“1/0”。因此,我们在XSD中保留了布尔值的字符串,并且该字符串将通过XMLAdapter对我们这边的marshal和unmarshal进行验证。XML适配器如下:

public class BooleanAdapter extends XmlAdapter<String, Boolean> 
{
@Override
    public Boolean unmarshal(String v) throws Exception
    {
        if(v.equalsIgnoreCase("true") || v.equals("1"))
        {
            return true;
        }
        else if(v.equalsIgnoreCase("false") || v.equals("0"))
        {
            return false;
        }
        else
        {
            throw new Exception("Boolean Value from XML File is Wrong.");
        }
    }

    @Override
    public String marshal(Boolean v) throws Exception
    {
        return v.toString();        
    }
}

The code above works in normal conditions, but when invalid data(eg: "abcd" or "") is read from xml file then the "throw new Exception();" is not getting propagated and the Unmarshal process moves on to read next node. I want the application to stop as soon as an exception is thrown. It seems my Exception is getting eaten away. Any help is much appreciated.

上面的代码在正常情况下可以工作,但是当从xml文件中读取无效数据(如:“abcd”或“”)时,“抛出新异常()”不会被传播,Unmarshal过程继续读取下一个节点。我希望应用程序在抛出异常后立即停止。看来我的例外正在逐渐消失。非常感谢您的帮助。

How to resolve this problem?

如何解决这个问题?

1 个解决方案

#1


19  

From the JavaDoc of XMLAdapter#unmarshal(ValueType):

来自XMLAdapter#unmarshal(ValueType)的JavaDoc:

Throws: java.lang.Exception - if there's an error during the conversion. The caller is responsible for reporting the error to the user through ValidationEventHandler.

抛出:. lang。异常-如果转换过程中出现错误。调用者负责通过ValidationEventHandler向用户报告错误。

So, yes - the exception is eaten and then reported using ValidationEventHandler, not thrown to the top of your stack.

所以,是的——异常被吃了,然后使用ValidationEventHandler报告,而不是扔到堆栈的顶部。

Check if you are already using any (custom, perhaps) ValidationEventHandler that groups your exceptions, or use DefaultValidationEventHandler, like this:

检查您是否已经在使用任何(可能是自定义的)将异常分组的ValidationEventHandler,或者使用DefaultValidationEventHandler,如下所示:

unmarshaller.setEventHandler(new DefaultValidationEventHandler());

It will cause unmarshalling failure on first error.

它将在第一个错误上导致反编组失败。

#1


19  

From the JavaDoc of XMLAdapter#unmarshal(ValueType):

来自XMLAdapter#unmarshal(ValueType)的JavaDoc:

Throws: java.lang.Exception - if there's an error during the conversion. The caller is responsible for reporting the error to the user through ValidationEventHandler.

抛出:. lang。异常-如果转换过程中出现错误。调用者负责通过ValidationEventHandler向用户报告错误。

So, yes - the exception is eaten and then reported using ValidationEventHandler, not thrown to the top of your stack.

所以,是的——异常被吃了,然后使用ValidationEventHandler报告,而不是扔到堆栈的顶部。

Check if you are already using any (custom, perhaps) ValidationEventHandler that groups your exceptions, or use DefaultValidationEventHandler, like this:

检查您是否已经在使用任何(可能是自定义的)将异常分组的ValidationEventHandler,或者使用DefaultValidationEventHandler,如下所示:

unmarshaller.setEventHandler(new DefaultValidationEventHandler());

It will cause unmarshalling failure on first error.

它将在第一个错误上导致反编组失败。