java-Jackson反序列化具有相同名称的xml字段

前端之家收集整理的这篇文章主要介绍了java-Jackson反序列化具有相同名称的xml字段 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想将来自HTTP请求的XML响应反序列化为POJO列表.我遇到的问题是XML对包含不同值的元素使用相同的名称属性”.

  1. <nowplaying-info-list>
  2. <nowplaying-info mountName="FGDGFD" timestamp="1559761606" type="track">
  3. <property name="cue_time_duration">
  4. <![CDATA[262000]]>
  5. </property>
  6. <property name="cue_time_start">
  7. <![CDATA[1559761571830]]>
  8. </property>
  9. <property name="cue_title">
  10. <![CDATA[Marine marchande]]>
  11. </property>
  12. <property name="track_album_name">
  13. <![CDATA[Octobre]]>
  14. </property>
  15. <property name="track_artist_name">
  16. <![CDATA[Les Cowboys Fringants]]>
  17. </property>
  18. <property name="track_id">
  19. <![CDATA[8133]]>
  20. </property>
  21. </property>
  22. </nowplaying-info>
  23. ...
  1. @JacksonXmlRootElement(localName = "nowplaying-info")
  2. public class ScheduleItem implements Serializable {
  3. @JacksonXmlProperty(localName = "property")
  4. private String song = null;
  5. private String artist = null;
  6. private String cover = null;
  7. private Long datetime = null;

我想将名称为cue_title的属性序列化为歌曲变量,并将cue_time_start命名为datetime.

最佳答案
字段和列表之间没有简单的映射.我建议创建一个单独的模型,将XML有效负载反序列化到它,然后再转换为所需的POJO.下面的示例显示了这个想法:

  1. import com.fasterxml.jackson.annotation.JsonProperty;
  2. import com.fasterxml.jackson.core.type.TypeReference;
  3. import com.fasterxml.jackson.databind.DeserializationFeature;
  4. import com.fasterxml.jackson.dataformat.xml.XmlMapper;
  5. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
  6. import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
  7. import java.io.File;
  8. import java.util.LinkedHashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.stream.Collectors;
  12. import org.apache.commons.lang3.StringUtils;
  13. public class XmlApp {
  14. public static void main(String[] args) throws Exception {
  15. System.out.println(new File(".").getAbsolutePath());
  16. File xml = new File("./src/main/resources/test.xml");
  17. XmlMapper xmlMapper = new XmlMapper();
  18. xmlMapper.setDefaultUseWrapper(false);
  19. xmlMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
  20. TypeReference<List<NowPlayingInfo>> type = new TypeReference<List<NowPlayingInfo>>() {
  21. };
  22. List<NowPlayingInfo> infos = xmlMapper.readValue(xml,type);
  23. List<ScheduleItem> items = infos.stream()
  24. .map(NowPlayingInfo::getPropertiesAsMap)
  25. .map(m -> {
  26. ScheduleItem item = new ScheduleItem();
  27. item.setSong(m.get("cue_title"));
  28. item.setDatetime(Long.parseLong(m.get("cue_time_start")));
  29. return item;
  30. }).collect(Collectors.toList());
  31. items.forEach(System.out::println);
  32. }
  33. }
  34. class ScheduleItem {
  35. private String song;
  36. private String artist;
  37. private String cover;
  38. private Long datetime;
  39. //getters,setters,toString
  40. }
  41. class NowPlayingInfo {
  42. private String mountName;
  43. private long timestamp;
  44. private String type;
  45. @JsonProperty("property")
  46. private List<Property> properties;
  47. public Map<String,String> getPropertiesAsMap() {
  48. Map<String,String> map = new LinkedHashMap<>();
  49. properties.forEach(p -> map.put(p.getName(),StringUtils.strip(p.getValue())));
  50. return map;
  51. }
  52. //getters,toString
  53. }
  54. class Property {
  55. @JacksonXmlText
  56. private String value;
  57. @JacksonXmlProperty(isAttribute = true)
  58. private String name;
  59. //getters,toString
  60. }

以上用于XML打印的应用程序:

  1. ScheduleItem{song='Marine marchande',artist='null',cover='null',datetime=1559761571830}

猜你在找的Spring相关文章