JAXB unmarshaller with custom interceptor?

天大地大妈咪最大 提交于 2020-01-05 07:43:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!