问题
Is it possible to "intercept" the unmarshalling process of JAXB?
I have an xml reponse that partially should be converted to a different java fields structure:
<xml>
<X_FIELD1></X_FIELD1>
<X_FIELD2></X_FIELD2>
...
<X_FIELD11></X_FIELD11>
</xml>
In my java class, I'd prefer to unmarshal this to a List<String>, instead of 11 String fields.
public class XmlResponse {
private String X_FIELD1;
private String X_FIELD2;
//...
private String X_FIELD11;
// private List<String> xFields;
}
But is that possible?
回答1:
You should use a custom xml adapter
http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/adapters/XmlJavaTypeAdapter.html
http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/adapters/XmlAdapter.html
apply it on the class level and implement the adapter.
来源:https://stackoverflow.com/questions/40280654/jaxb-unmarshaller-with-custom-interceptor