The Java – JAXB xmladapter method does not throw an exception

I am using JAXB xmladapter to organize and ungroup Boolean values The application's XML file will also be accessed by the c# application We have to validate the XML file, which is done using XSD C# application writes "true" value for Boolean node However, the validation of our XSD is the same because it only allows "true / false" or "1 / 0" So we keep the Boolean value of string in XSD, and the string will be verified by xmladapter to organize and ungroup

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 above code works under normal conditions, but when reading invalid data from an XML file (for example: "ABCD" or ""), "throw new exception();" Without being propagated, the unmarshal process continues to read the next node Once an exception is thrown, I want the application to stop It seems that my abnormality has been eaten Thank you for any help

How to solve this problem?

Solution

Javadoc from xmladapter #unmarshal (ValueType):

So, yes - the exception is eaten and reported using the validationeventhandler instead of being thrown to the top of your stack

Check that you have used any (custom) validationeventhandler to combine your exceptions, or defaultvalidationeventhandler, as follows:

unmarshaller.setEventHandler(new DefaultValidationEventHandler());

The first error causes the ungroup to fail

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>