解组对象时 JAXB 不调用 setter
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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).