{"id":877,"date":"2023-03-09T21:49:34","date_gmt":"2023-03-09T13:49:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=877"},"modified":"2023-04-29T16:41:58","modified_gmt":"2023-04-29T08:41:58","slug":"spring-boot-integrate-xstream-to-parse-multi-level-complex-data","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/09\/spring-boot-integrate-xstream-to-parse-multi-level-complex-data\/","title":{"rendered":"Spring Boot\u96c6\u6210XStream\u89e3\u6790\u591a\u5c42\u7ea7\u590d\u6742\u6570\u636e"},"content":{"rendered":"<p>\u4ee5\u652f\u4ed8\u5b9dAPI\u8fd4\u56de\u6570\u636e\u4e3a\u4f8b<\/p>\n<h2>\u8fd4\u56dexml<\/h2>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\n\n&lt;alipay&gt;\n  &lt;is_success&gt;T&lt;\/is_success&gt;\n  &lt;request&gt;\n    &lt;param name=&quot;extend_params&quot;&gt;{&quot;secondary_merchant_id&quot;:&quot;A80001&quot;,&quot;secondary_merchant_industry&quot;:&quot;7011&quot;,&quot;secondary_merchant_name&quot;:&quot;Muku&quot;,&quot;store_id&quot;:&quot;BJ_ZZ_001&quot;,&quot;store_name&quot;:&quot;Muku in the Dreieichstrabe&quot;}&lt;\/param&gt;\n    &lt;param name=&quot;_input_charset&quot;&gt;UTF-8&lt;\/param&gt;\n    &lt;param name=&quot;subject&quot;&gt;Payment by QR-Code&lt;\/param&gt;\n    &lt;param name=&quot;sign&quot;&gt;784a5f3087aec1eb397ddd3f70280b48&lt;\/param&gt;\n    &lt;param name=&quot;notify_url&quot;&gt;http:\/\/www.yezhou.cc\/notify\/alipay_global_notify.php&lt;\/param&gt;\n    &lt;param name=&quot;product_code&quot;&gt;OVERSEAS_MBARCODE_PAY&lt;\/param&gt;\n    &lt;param name=&quot;out_trade_no&quot;&gt;20190327100628310&lt;\/param&gt;\n    &lt;param name=&quot;trans_currency&quot;&gt;THB&lt;\/param&gt;\n    &lt;param name=&quot;partner&quot;&gt;2088621924156138&lt;\/param&gt;\n    &lt;param name=&quot;service&quot;&gt;alipay.acquire.precreate&lt;\/param&gt;\n    &lt;param name=&quot;total_fee&quot;&gt;11.0&lt;\/param&gt;\n    &lt;param name=&quot;currency&quot;&gt;THB&lt;\/param&gt;\n    &lt;param name=&quot;sign_type&quot;&gt;MD5&lt;\/param&gt;\n    &lt;param name=&quot;timestamp&quot;&gt;1553652388309&lt;\/param&gt;\n  &lt;\/request&gt;\n  &lt;response&gt;\n    &lt;alipay&gt;\n      &lt;big_pic_url&gt;http:\/\/mobilecodec.alipaydev.com\/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=L&lt;\/big_pic_url&gt;\n      &lt;out_trade_no&gt;20190327100628310&lt;\/out_trade_no&gt;\n      &lt;pic_url&gt;http:\/\/mobilecodec.alipaydev.com\/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=M&lt;\/pic_url&gt;\n      &lt;qr_code&gt;https:\/\/qr.alipay.com\/bax04397fxtnhmvfaqkd0037&lt;\/qr_code&gt;\n      &lt;result_code&gt;SUCCESS&lt;\/result_code&gt;\n      &lt;small_pic_url&gt;http:\/\/mobilecodec.alipaydev.com\/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=S&lt;\/small_pic_url&gt;\n      &lt;voucher_type&gt;qrcode&lt;\/voucher_type&gt;\n    &lt;\/alipay&gt;\n  &lt;\/response&gt;\n  &lt;sign&gt;6585d039857aa946e11f289cdf127002&lt;\/sign&gt;\n  &lt;sign_type&gt;MD5&lt;\/sign_type&gt;\n&lt;\/alipay&gt;<\/code><\/pre>\n<h2>\u6620\u5c04Bean<\/h2>\n<pre><code class=\"language-java\">import com.thoughtworks.xstream.annotations.XStreamAlias;\nimport com.thoughtworks.xstream.annotations.XStreamAsAttribute;\nimport com.thoughtworks.xstream.annotations.XStreamConverter;\nimport com.thoughtworks.xstream.annotations.XStreamImplicit;\nimport com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;\nimport lombok.Data;\nimport lombok.NoArgsConstructor;\n\nimport java.util.List;\n\n@Data\n@NoArgsConstructor\n@XStreamAlias(&quot;alipay&quot;)\npublic class AlipayCreateReturn {\n\n    @XStreamAlias(&quot;is_success&quot;)\n    private String isSuccess;\n\n    @XStreamAlias(&quot;error&quot;)\n    private String error;\n\n    @XStreamAlias(&quot;request&quot;)\n    private Request request;\n\n    @XStreamAlias(&quot;response&quot;)\n    private Response response;\n\n    @XStreamAlias(&quot;sign_type&quot;)\n    private String signType;\n\n    private String sign;\n\n    @Data\n    @NoArgsConstructor\n    public static class Request {\n        @XStreamImplicit(itemFieldName = &quot;param&quot;)\n        private List&lt;Param&gt; param;\n    }\n\n    @Data\n    @NoArgsConstructor\n    @XStreamAlias(&quot;param&quot;)\n    @XStreamConverter(value = ToAttributedValueConverter.class, strings = {&quot;content&quot;})\n    public static class Param {\n        @XStreamAsAttribute\n        private String name;\n        private String content;\n    }\n\n    @Data\n    @NoArgsConstructor\n    public static class Response {\n        @XStreamAlias(&quot;alipay&quot;)\n        private AlipayCreateResponse createResponse;\n    }\n\n    @Data\n    @NoArgsConstructor\n    public static class AlipayCreateResponse {\n        @XStreamAlias(&quot;result_code&quot;)\n        private String resultCode;\n        @XStreamAlias(&quot;detail_error_code&quot;)\n        private String detailErrorCode;\n        @XStreamAlias(&quot;detail_error_des&quot;)\n        private String detailErrorDes;\n        @XStreamAlias(&quot;display_message&quot;)\n        private String displayMessage;\n\n        @XStreamAlias(&quot;out_trade_no&quot;)\n        private String outTradeNo;\n        @XStreamAlias(&quot;qr_code&quot;)\n        private String qrCode;\n        @XStreamAlias(&quot;big_pic_url&quot;)\n        private String bigPicUrl;\n        @XStreamAlias(&quot;pic_url&quot;)\n        private String picUrl;\n        @XStreamAlias(&quot;small_pic_url&quot;)\n        private String smallPicUrl;\n        @XStreamAlias(&quot;voucher_type&quot;)\n        private String voucherType;\n    }\n}<\/code><\/pre>\n<h2>\u6d4b\u8bd5<\/h2>\n<pre><code class=\"language-java\">@Test\npublic void parseAlipayCreateReturn() {\n    String xml = &quot;&lt;?xml version=\\&quot;1.0\\&quot; encoding=\\&quot;utf-8\\&quot;?&gt;\\n&quot; +\n            &quot;\\n&quot; +\n            &quot;&lt;alipay&gt;\\n&quot; +\n            &quot;  &lt;is_success&gt;T&lt;\/is_success&gt;\\n&quot; +\n            &quot;  &lt;request&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;extend_params\\&quot;&gt;{\\&quot;secondary_merchant_id\\&quot;:\\&quot;A80001\\&quot;,\\&quot;secondary_merchant_industry\\&quot;:\\&quot;7011\\&quot;,\\&quot;secondary_merchant_name\\&quot;:\\&quot;Muku\\&quot;,\\&quot;store_id\\&quot;:\\&quot;BJ_ZZ_001\\&quot;,\\&quot;store_name\\&quot;:\\&quot;Muku in the Dreieichstrabe\\&quot;}&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;_input_charset\\&quot;&gt;UTF-8&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;subject\\&quot;&gt;Payment by QR-Code&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;sign\\&quot;&gt;784a5f3087aec1eb397ddd3f70280b48&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;notify_url\\&quot;&gt;http:\/\/www.yezhou.cc\/notify\/alipay_global_notify.php&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;product_code\\&quot;&gt;OVERSEAS_MBARCODE_PAY&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;out_trade_no\\&quot;&gt;20190327100628310&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;trans_currency\\&quot;&gt;THB&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;partner\\&quot;&gt;2088621924156138&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;service\\&quot;&gt;alipay.acquire.precreate&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;total_fee\\&quot;&gt;11.0&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;currency\\&quot;&gt;THB&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;sign_type\\&quot;&gt;MD5&lt;\/param&gt;\\n&quot; +\n            &quot;    &lt;param name=\\&quot;timestamp\\&quot;&gt;1553652388309&lt;\/param&gt;\\n&quot; +\n            &quot;  &lt;\/request&gt;\\n&quot; +\n            &quot;  &lt;response&gt;\\n&quot; +\n            &quot;    &lt;alipay&gt;\\n&quot; +\n            &quot;      &lt;big_pic_url&gt;http:\/\/mobilecodec.alipaydev.com\/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=L&lt;\/big_pic_url&gt;\\n&quot; +\n            &quot;      &lt;out_trade_no&gt;20190327100628310&lt;\/out_trade_no&gt;\\n&quot; +\n            &quot;      &lt;pic_url&gt;http:\/\/mobilecodec.alipaydev.com\/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=M&lt;\/pic_url&gt;\\n&quot; +\n            &quot;      &lt;qr_code&gt;https:\/\/qr.alipay.com\/bax04397fxtnhmvfaqkd0037&lt;\/qr_code&gt;\\n&quot; +\n            &quot;      &lt;result_code&gt;SUCCESS&lt;\/result_code&gt;\\n&quot; +\n            &quot;      &lt;small_pic_url&gt;http:\/\/mobilecodec.alipaydev.com\/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=S&lt;\/small_pic_url&gt;\\n&quot; +\n            &quot;      &lt;voucher_type&gt;qrcode&lt;\/voucher_type&gt;\\n&quot; +\n            &quot;    &lt;\/alipay&gt;\\n&quot; +\n            &quot;  &lt;\/response&gt;\\n&quot; +\n            &quot;  &lt;sign&gt;6585d039857aa946e11f289cdf127002&lt;\/sign&gt;\\n&quot; +\n            &quot;  &lt;sign_type&gt;MD5&lt;\/sign_type&gt;\\n&quot; +\n            &quot;&lt;\/alipay&gt;&quot;;\n    log.info(xml);\n    XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder(&quot;_-&quot;, &quot;_&quot;))); \/\/\u89e3\u51b3\u4e0b\u5212\u7ebf\u95ee\u9898\n    \/\/com.thoughtworks.xstream.mapper.CannotResolveClassException: alipay\n    xStream.alias(&quot;alipay&quot;, AlipayCreateReturn.class);\n    \/\/com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field\n    xStream.autodetectAnnotations(true); \/\/\u81ea\u52a8\u68c0\u6d4b\u6ce8\u89e3\n    xStream.processAnnotations(AlipayCreateReturn.class); \/\/\u5e94\u7528Bean\u7c7b\u7684\u6ce8\u89e3\n    AlipayCreateReturn alipayCreateReturn = (AlipayCreateReturn) xStream.fromXML(xml);\n    log.info(alipayCreateReturn.toString());\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u652f\u4ed8\u5b9dAPI\u8fd4\u56de\u6570\u636e\u4e3a\u4f8b \u8fd4\u56dexml &lt;?xml version=&quot;1.0&quot; en [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[106],"class_list":["post-877","post","type-post","status-publish","format-standard","hentry","category-spring-boot","tag-xstream"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=877"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/877\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}