解组对象时 JAXB 不调用 setter

发布于 2024-08-30 08:12:47 字数 2046 浏览 4 评论 0原文

我使用 JAXB 2.0 JDK 6 来将 XML 实例解组到 POJO 中。

为了添加一些自定义验证,我已将验证调用插入到属性的 setter 中,但尽管它是私有的,但解组器似乎不会调用 setter 而是直接修改私有字段。

对我来说至关重要的是,每次解组调用都会针对该特定字段进行自定义验证。

我应该怎么办?

代码:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LegalParams", propOrder = {
    "value"
})
public class LegalParams {

    private static final Logger LOG = Logger.getLogger(LegalParams.class);

    @XmlTransient
    private LegalParamsValidator legalParamValidator;

    public LegalParams() {

        try {
            WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
            LegalParamsFactory legalParamsFactory = (LegalParamsFactory) webApplicationContext.getBean("legalParamsFactory");
            HttpSession httpSession = SessionHolder.getInstance().get();
            legalParamValidator = legalParamsFactory.newLegalParamsValidator(httpSession);
        }
        catch (LegalParamsException lpe) {
            LOG.warn("Validator related error occurred while attempting to construct a new instance of LegalParams");
            throw new IllegalStateException("LegalParams creation failure", lpe);
        }
        catch (Exception e) {
            LOG.warn("Spring related error occurred while attempting to construct a new instance of LegalParams");
            throw new IllegalStateException("LegalParams creation failure", e);
        }
    }

    @XmlValue
    private String value;

    /**
     * Gets the value of the value property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *
     */
    public String getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     * @throws TestCaseValidationException
     *
     */
    public void setValue(String value) throws TestCaseValidationException {
        legalParamValidator.assertValid(value);
        this.value = value;
    }
}

I am using JAXB 2.0 JDK 6 in order to unmarshall an XML instance into POJOs.

In order to add some custom validation I have inserted a validation call into the setter of a property, yet despite it being private, it seems that the unmarshaller does not call the setter but directly modifies the private field.

It is crucial to me that the custom validation occurs for this specific field every unmarshall call.

What should I do?

Code:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "LegalParams", propOrder = {
    "value"
})
public class LegalParams {

    private static final Logger LOG = Logger.getLogger(LegalParams.class);

    @XmlTransient
    private LegalParamsValidator legalParamValidator;

    public LegalParams() {

        try {
            WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
            LegalParamsFactory legalParamsFactory = (LegalParamsFactory) webApplicationContext.getBean("legalParamsFactory");
            HttpSession httpSession = SessionHolder.getInstance().get();
            legalParamValidator = legalParamsFactory.newLegalParamsValidator(httpSession);
        }
        catch (LegalParamsException lpe) {
            LOG.warn("Validator related error occurred while attempting to construct a new instance of LegalParams");
            throw new IllegalStateException("LegalParams creation failure", lpe);
        }
        catch (Exception e) {
            LOG.warn("Spring related error occurred while attempting to construct a new instance of LegalParams");
            throw new IllegalStateException("LegalParams creation failure", e);
        }
    }

    @XmlValue
    private String value;

    /**
     * Gets the value of the value property.
     *
     * @return
     *     possible object is
     *     {@link String }
     *
     */
    public String getValue() {
        return value;
    }

    /**
     * Sets the value of the value property.
     *
     * @param value
     *     allowed object is
     *     {@link String }
     * @throws TestCaseValidationException
     *
     */
    public void setValue(String value) throws TestCaseValidationException {
        legalParamValidator.assertValid(value);
        this.value = value;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-09-06 08:12:47

JAXB 使用字段访问,因为您通过使用 @XmlValue 注解字段并声明 @XmlAccessorType(XmlAccessType.FIELD) 将其配置为使用字段访问。

要使用属性访问,您可以将 @XmlValue 移至 getter 或 setter(根本不需要 @XmlAccessorType)。

JAXB uses field access because you configured it to use field access by annotating a field with @XmlValue and by declaring @XmlAccessorType(XmlAccessType.FIELD).

To use property access, you can move @XmlValue to either getter or setter (@XmlAccessorType is not needed at all).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文