如何使用setter强制JAXB Unmarshal? [英] How to force JAXB Unmarshal using setters?

查看:97
本文介绍了如何使用setter强制JAXB Unmarshal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解组时,我需要验证值是否在一定范围内。我打算在setter中添加这个验证。如何在解组时强制JAXB使用setter?在此先感谢。

While unmarshalling, I need to validate whether value is within certain range. I am planning to add this validation in setter. How can I force JAXB to use setter while unmarshalling? Thanks in advance.

推荐答案

您需要执行以下操作:


  1. 确保映射属性(get / set方法)而不是字段(实例变量)
  1. Make sure you are mapping the property (get/set methods) and not the field (instance variable)

  1. 确保您没有使用<$你班上的c $ c> @XmlAccessorType(XmlAccessType.FIELD)。

  2. 对于注释,你确实提供了把它们放在get(或set)方法而不是


  • 你有一个正确匹配的获取/设置对

  • You have a properly matched get/set pair

    public String getFoo() {
        return this.foo;
    } 
    
    public void setFoo(String foo) {
        this.foo = foo;
    }
    


  • 对于集合属性,只有在具有值的情况下才会调用该集合 null 在对象实例化后首次调用 get 时(即您没有将其初始化为空集合) 。

  • For a collection property the set will only be called if it has a value of null when the get is first called after the object is instantiated (i.e. you are not initializing it to an empty collection).
  • 这篇关于如何使用setter强制JAXB Unmarshal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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