为什么JAXB 2.0默认验证的特定行为发生了变化?

时间:2015-05-27 08:44:03

标签: java xml java-ee jaxb

Java EE人群中的任何人都可以告诉我,那些时候使验证更加宽松并且不会因错误的XML而失败的原因是什么?

在文档中找到了说明,但没有背景信息......

  

由于在JAXB 2.0中定义了解组无效XML内容,因此Unmarshaller默认验证事件处理程序比JAXB 1.0更宽松。当JAXB 1.0绑定编译器生成的模式派生代码向JAXBContext注册时,默认的unmarshal验证处理程序是DefaultValidationEventHandler,并在遇到致命错误或错误后终止编组操作。对于JAXB 2.0客户端应用程序,没有显式定义的默认验证处理程序,默认事件处理仅在遇到致命错误后终止解组操作。 from: Interface Unmarshaller

修改
我将提供一个例子..

Unmarshaller unmarshaller = getJaxbContext(packages).createUnmarshaller();
// sets event handler to DefaultValidationEventHandler  
// (error and fatal error lead to an exception)
// **DefaultValidationEventHandler is not default by default anymore**
unmarshaller.setEventHandler(null);
Object o = unmarshaller.unmarshal(file);

这是Setter的rt(1.7.0_60)实现,名为:

/**
 * Allow an application to register a validation event handler.
 * <p>
 * The validation event handler will be called by the JAXB Provider if any
 * validation errors are encountered during calls to any of the
 * <tt>unmarshal</tt> methods.  If the client application does not register
 * a validation event handler before invoking the unmarshal methods, then
 * all validation events will be silently ignored and may result in
 * unexpected behaviour.
 *
 * @param handler the validation event handler
 * @throws JAXBException if an error was encountered while setting the
 *        event handler
 */
public void setEventHandler(ValidationEventHandler handler)
    throws JAXBException {

    if( handler == null ) {
        eventHandler = new DefaultValidationEventHandler();
    } else {
        eventHandler = handler;
    }
}

他们为什么改变主意?

0 个答案:

没有答案