记录自己踩的坑
需要转化的xml报文
<rtn>
<code>0</code>
<msg>查询成功</msg>
<tel>15904084808</tel>
<channel>HZXY</channel>
<activitylist>
<activity>
<activityid>21279</activityid>
<name>3元1G流量日包</name>
<begintime>1607356800</begintime>
<endtime>2238508800</endtime>
</activity>
</activitylist>
</rtn>
首先xml工具类,用于xml到对象的转化
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.util.Map;
import java.util.Set;
public class XmlUtil {
public static Object xmlToBean(String xml, String root, Class clazz) {
XStream xstream = new XStream(new DomDriver());
xstream.alias(root, clazz);
return xstream.fromXML(xml);
}
public static Object xmlToBean(String xml, Map<String, Class> clazzMap) {
XStream xstream = new XStream(new DomDriver());
Set<Map.Entry<String, Class>> entries = clazzMap.entrySet();
for (Map.Entry<String, Class> entry : entries) {
xstream.alias(entry.getKey(), entry.getValue());
}
return xstream.fromXML(xml);
}
}
然后就是dto定义,分为两层,之前没搞对就是因为分了三层
import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@XStreamAlias("rtn")
@Data
public class LnQueryProductRtnDro {
private String code;
private String msg;
private String tel;
private String channel;
@XStreamAlias("activitylist")
private List<LnActivityDto> activitylist = new ArrayList<>();
}