Tôi đang sử dụng JAXB 2.0 JDK 6 để unmarshall một thể hiện XML vào POJOs. Để thêm một số xác nhận tùy chỉnh, tôi đã chèn một cuộc gọi xác nhận vào setter của một thuộc tính, mặc dù nó là riêng tư, có vẻ như unmarshaller không gọi setter nhưng trực tiếp sửa đổi trường riêng.JAXB không gọi setter khi unmarshalling đối tượng
Điều quan trọng đối với tôi là việc xác thực tùy chỉnh xảy ra cho trường cụ thể này mọi cuộc gọi không được sửa đổi.
Tôi nên làm gì?
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;
}
}
Cảm ơn mẹo đã làm :) – Yaneeve