自定义JAXB unmarshall进程的错误处理 [英] Customizing error handling of JAXB unmarshall process

查看:145
本文介绍了自定义JAXB unmarshall进程的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个描述根元素类 Root 的模式,其中包含 List< Entry> 其中条目类具有必填字段名称。

Assuming I have a schema that describes a root element class Root that contains a List<Entry> where the Entry class has a required field name.

以下是代码的外观:

@XmlRootElement 
class Root{
  @XmlElement(name="entry")
  public List<Entry> entries = Lists.newArrayList();
}

@XmlRootElement 
class Entry{
  @XmlElement(name="name",required=true)
  public String name;
}

如果我提供以下用于解组的XML:

If I supply the following XML for unmarshalling:

<root>
  <entry>
    <name>ekeren</name>
  </entry>
  <entry>
  </entry>
</root>

我遇到问题,因为第二个条目不包含名称。所以unmarshall生成 null

I have a problem because the second entry does not contain a name. So unmarshall produces null.

有没有办法自定义JAXB以解组 Root 只包含好条目的对象?

Is there a way to customize JAXB to unmarshall a Root object that will only contain the "good" entry?

推荐答案

你可以添加魔术后的unmarshal方法来照顾空条目:

You could add the magic afterUnmarshal method to take care of empty entries:

@XmlRootElement 
class Root{
  @XmlElement(name="entry")
  public List<Entry> entries = Lists.newArrayList();

  void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
    Iterator<Entry> iter = entries.iterator();
    while (iter.hasNext()) {
      if (iter.next().name == null) iter.remove();
    }
  }
}

编辑:

不确定这是否更适合你,但也许它有所帮助。您也可以使用Pacher,例如如果不是你需要修复/验证你的结果的所有对象都可以在afterUnmarshal(..)

Not sure if this is better suited for you, but maybe it's of help. You could also use a Pacher, e.g. if not all objects you need to fix/validate your result are available in afterUnmarshal(..)


在所有解析之后运行UnmarshallingContext完成。主要用于解析转发IDREF,但它可以运行任何操作。 (javadoc)

Runs by UnmarshallingContext after all the parsing is done. Primarily used to resolve forward IDREFs, but it can run any action. (javadoc)

以下是一个例子:

@XmlRootElement 
class Entry{
  @XmlElement(name="name",required=true)
  public String name;

  private boolean isValidEntry() {
    return name != null;
  }

  void afterUnmarshal(final Unmarshaller unmarshaller, final Object parent) {
    if (!isValidEntry()) {
      // entry not yet added to parent - use a patcher
      UnmarshallingContext.getInstance().addPatcher(new Patcher() {
        public void run() throws SAXException {
          ((Root)parent).removeEntry(this);
        }
      });
    }
  }
}

我不会滥用它太多了,不仅因为它只是Sun-only API。

I wouldn't abuse it too much though, not only as it is Sun-only API.

但是,如果你真的在寻找可配置的东西,而不是编组代码的一部分对象本身。解组后最好看一些东西。我想知道 Bean Validation(JSR 303)是不是一个完美的选择对你而言,例如使用 Hibernate Validator (不要被名字吓倒,你不需要Hibernate ORM使用它)。我自己没有使用它,但使用(新)标准进行验证听起来合理,不是吗?

But if you're really looking into something configurable that isn't part of the code of the marshalled objects itself. It might be best to look at something after unmarshalling. I wonder if Bean Validation (JSR 303) wouldn't be a perfect fit for you, e.g. using Hibernate Validator (don't get intimidated by the name, you don't need Hibernate ORM to use it). I haven't used it myself, but using a (new) standard for validation sounds reasonable, doesn't it?

这篇关于自定义JAXB unmarshall进程的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆