JAXB does not call setter when unmarshalling objects

Posted by Yaneeve on Stack Overflow See other posts from Stack Overflow or by Yaneeve
Published on 2010-04-22T11:10:24Z Indexed on 2010/04/22 11:13 UTC
Read the original article Hit count: 385

Filed under:
|
|
|

Hi all,

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;
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about jaxb2