我们经常会有需求,比如调用别人的接口,别人会返回给你json或者xml格式的数据,然后我们要转换为相应的对象供我们自己使用;
也有的时候是别人的调我们的接口,让我们提供xml格式的数据或者json格式的数据,json的话这里都不说了,很好处理,甚至springMvc本身就很容易支持。
下面只介绍xml和对象彼此转换
首先我们需要引入的jar包:xstream-1.4.8.jar
下面直接贴代码,代码是在springMvc环境下写的
<code class="language-java hljs has-numbering"><span class="hljs-annotation">@Controller</span> <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NavigationAppDataController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseController</span>{</span> <span class="hljs-annotation">@SuppressWarnings</span>(<span class="hljs-string">"unused"</span>) <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Logger log = LoggerFactory.getLogger(NavigationAppDataController.class); <span class="hljs-annotation">@Autowired</span> <span class="hljs-keyword">private</span> StoNavigationAppManager stoNavigationAppManager; <span class="hljs-annotation">@RequestMapping</span>(value = <span class="hljs-string">"/appsite/getNavigationXmlData.htm"</span>) <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getNavigationXmlData</span>(HttpServletRequest request,HttpServletResponse response){ List<NavigationAppCommand> findStoreNavigationAppToXml = stoNavigationAppManager.findStoreNavigationAppData(); XStreamConfig xStreamConfig = <span class="hljs-keyword">new</span> XStreamConfig(); <span class="hljs-comment">// 这里是什么意思我下面文字中会介绍,大概就是对xml中节点进行重命名的</span> xStreamConfig.getAliasMap().put(<span class="hljs-string">"topcategories"</span>, List.class); xStreamConfig.getAliasMap().put(<span class="hljs-string">"category"</span>, NavigationAppCommand.class); String xml = toXML(findStoreNavigationAppToXml, xStreamConfig); <span class="hljs-comment">// 设置返回数据的编码格式</span> response.setCharacterEncoding(CharsetType.UTF8); <span class="hljs-comment">// 设置内容类型,决定浏览器将以什么类型读我们返回的数据进行展示</span> response.setContentType(<span class="hljs-string">"application/xml"</span>); <span class="hljs-comment">// 将数据写入response返回给请求端</span> response.getWriter().write(xml); } <span class="hljs-javadoc">/** * 将bean对象根据xStreamConfig格式转成xml格式的字符串返回 * *<span class="hljs-javadoctag"> @param</span> bean * 对象 *<span class="hljs-javadoctag"> @param</span> xStreamConfig * 配置 *<span class="hljs-javadoctag"> @return</span> */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> String <span class="hljs-title">toXML</span>(Object bean,XStreamConfig xStreamConfig){ XStream xstream = constructXStream(xStreamConfig); <span class="hljs-keyword">return</span> xstream.toXML(bean); } <span class="hljs-javadoc">/** * 将xml字符串转为相对应的对象 * *<span class="hljs-javadoctag"> @param</span> xml *<span class="hljs-javadoctag"> @return</span> */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <T> T <span class="hljs-title">fromXML</span>(String xml){ <span class="hljs-keyword">return</span> fromXML(xml, <span class="hljs-keyword">null</span>); } <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <T> T <span class="hljs-title">fromXML</span>(String xml,XStreamConfig xStreamConfig){ XStream xstream = constructXStream(xStreamConfig); <span class="hljs-keyword">return</span> (T) xstream.fromXML(xml); } <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> XStream <span class="hljs-title">constructXStream</span>(XStreamConfig xStreamConfig){ XStream xstream = <span class="hljs-keyword">new</span> XStream(); <span class="hljs-keyword">if</span> (Validator.isNotNullOrEmpty(xStreamConfig)){ <span class="hljs-comment">// *******************alias********************************************</span> Map<String, Class<?>> aliasMap = xStreamConfig.getAliasMap(); <span class="hljs-keyword">if</span> (Validator.isNotNullOrEmpty(aliasMap)){ <span class="hljs-keyword">for</span> (Map.Entry<String, Class<?>> entry : aliasMap.entrySet()){ String key = entry.getKey(); Class<?> value = entry.getValue(); <span class="hljs-comment">// 类重命名</span> xstream.alias(key, value); } } <span class="hljs-comment">// *******************implicitCollectionMap********************************************</span> Map<String, Class<?>> implicitCollectionMap = xStreamConfig.getImplicitCollectionMap(); <span class="hljs-keyword">if</span> (Validator.isNotNullOrEmpty(implicitCollectionMap)){ <span class="hljs-keyword">for</span> (Map.Entry<String, Class<?>> entry : implicitCollectionMap.entrySet()){ String key = entry.getKey(); Class<?> value = entry.getValue(); xstream.addImplicitCollection(value, key); } } } } }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li><li>51</li><li>52</li><li>53</li><li>54</li><li>55</li><li>56</li><li>57</li><li>58</li><li>59</li><li>60</li><li>61</li><li>62</li><li>63</li><li>64</li><li>65</li><li>66</li><li>67</li><li>68</li><li>69</li><li>70</li><li>71</li><li>72</li><li>73</li><li>74</li><li>75</li><li>76</li><li>77</li><li>78</li><li>79</li><li>80</li><li>81</li></ul>
<code class="language-java hljs has-numbering"><span class="hljs-javadoc">/** * XStream相关配置. * */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">XStreamConfig</span>{</span> <span class="hljs-javadoc">/** 别名. */</span> <span class="hljs-keyword">private</span> Map<String, Class<?>> aliasMap = <span class="hljs-keyword">new</span> HashMap<String, Class<?>>(); <span class="hljs-javadoc">/** 隐式集合 隐藏,隐藏,比如下面有list,泛型中的第二个参数 Class 是 ownerType. */</span> <span class="hljs-keyword">private</span> Map<String, Class<?>> implicitCollectionMap = <span class="hljs-keyword">new</span> HashMap<String, Class<?>>(); <span class="hljs-javadoc">/** * 获得 别名. * *<span class="hljs-javadoctag"> @return</span> the aliasMap */</span> <span class="hljs-keyword">public</span> Map<String, Class<?>> <span class="hljs-title">getAliasMap</span>(){ <span class="hljs-keyword">return</span> aliasMap; } <span class="hljs-javadoc">/** * 设置 别名. * *<span class="hljs-javadoctag"> @param</span> aliasMap * the aliasMap to set */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAliasMap</span>(Map<String, Class<?>> aliasMap){ <span class="hljs-keyword">this</span>.aliasMap = aliasMap; } <span class="hljs-javadoc">/** * 获得 隐式集合 隐藏,隐藏,比如下面有list,泛型中的第二个参数 Class 是 ownerType. * *<span class="hljs-javadoctag"> @return</span> the implicitCollectionMap */</span> <span class="hljs-keyword">public</span> Map<String, Class<?>> <span class="hljs-title">getImplicitCollectionMap</span>(){ <span class="hljs-keyword">return</span> implicitCollectionMap; } <span class="hljs-javadoc">/** * 设置 隐式集合 隐藏,隐藏,比如下面有list,泛型中的第二个参数 Class 是 ownerType. * *<span class="hljs-javadoctag"> @param</span> implicitCollectionMap * the implicitCollectionMap to set */</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setImplicitCollectionMap</span>(Map<String, Class<?>> implicitCollectionMap){ <span class="hljs-keyword">this</span>.implicitCollectionMap = implicitCollectionMap; } }</code><ul style="display: block;" class="pre-numbering"><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li><li>11</li><li>12</li><li>13</li><li>14</li><li>15</li><li>16</li><li>17</li><li>18</li><li>19</li><li>20</li><li>21</li><li>22</li><li>23</li><li>24</li><li>25</li><li>26</li><li>27</li><li>28</li><li>29</li><li>30</li><li>31</li><li>32</li><li>33</li><li>34</li><li>35</li><li>36</li><li>37</li><li>38</li><li>39</li><li>40</li><li>41</li><li>42</li><li>43</li><li>44</li><li>45</li><li>46</li><li>47</li><li>48</li><li>49</li><li>50</li></ul>
实现方法很简单另外这里说一下response的几个参数设置的意思。
-
response.setCharacterEncoding(CharsetType.UTF8);
设置返回数据的编码格式如果不设置这个,返回到客户请求端的数据可能会乱码,这个编码格式一定要统一掉 -
response.setContentType(“application/xml”);
设置内容类型,决定浏览器将以什么类型读我们返回的数据进行展示,如果不设置,那么浏览器显示的就是一大坨xml格式的字符串,没有任何效果,而设置之后就可以像我上面截图的那样,节点可以点击展开和关闭,并且数据和节点颜色对比明显,便于观看。