Spring Boot集成XStream解析多层级复杂数据

以支付宝API返回数据为例

返回xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>

<alipay>
<is_success>T</is_success>
<request>
<param name="extend_params">{"secondary_merchant_id":"A80001","secondary_merchant_industry":"7011","secondary_merchant_name":"Muku","store_id":"BJ_ZZ_001","store_name":"Muku in the Dreieichstrabe"}</param>
<param name="_input_charset">UTF-8</param>
<param name="subject">Payment by QR-Code</param>
<param name="sign">784a5f3087aec1eb397ddd3f70280b48</param>
<param name="notify_url">http://www.yezhou.cc/notify/alipay_global_notify.php</param>
<param name="product_code">OVERSEAS_MBARCODE_PAY</param>
<param name="out_trade_no">20190327100628310</param>
<param name="trans_currency">THB</param>
<param name="partner">2088621924156138</param>
<param name="service">alipay.acquire.precreate</param>
<param name="total_fee">11.0</param>
<param name="currency">THB</param>
<param name="sign_type">MD5</param>
<param name="timestamp">1553652388309</param>
</request>
<response>
<alipay>
<big_pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=L</big_pic_url>
<out_trade_no>20190327100628310</out_trade_no>
<pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=M</pic_url>
<qr_code>https://qr.alipay.com/bax04397fxtnhmvfaqkd0037</qr_code>
<result_code>SUCCESS</result_code>
<small_pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=S</small_pic_url>
<voucher_type>qrcode</voucher_type>
</alipay>
</response>
<sign>6585d039857aa946e11f289cdf127002</sign>
<sign_type>MD5</sign_type>
</alipay>

映射Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@XStreamAlias("alipay")
public class AlipayCreateReturn {

@XStreamAlias("is_success")
private String isSuccess;

@XStreamAlias("error")
private String error;

@XStreamAlias("request")
private Request request;

@XStreamAlias("response")
private Response response;

@XStreamAlias("sign_type")
private String signType;

private String sign;

@Data
@NoArgsConstructor
public static class Request {
@XStreamImplicit(itemFieldName = "param")
private List<Param> param;
}

@Data
@NoArgsConstructor
@XStreamAlias("param")
@XStreamConverter(value = ToAttributedValueConverter.class, strings = {"content"})
public static class Param {
@XStreamAsAttribute
private String name;
private String content;
}

@Data
@NoArgsConstructor
public static class Response {
@XStreamAlias("alipay")
private AlipayCreateResponse createResponse;
}

@Data
@NoArgsConstructor
public static class AlipayCreateResponse {
@XStreamAlias("result_code")
private String resultCode;
@XStreamAlias("detail_error_code")
private String detailErrorCode;
@XStreamAlias("detail_error_des")
private String detailErrorDes;
@XStreamAlias("display_message")
private String displayMessage;

@XStreamAlias("out_trade_no")
private String outTradeNo;
@XStreamAlias("qr_code")
private String qrCode;
@XStreamAlias("big_pic_url")
private String bigPicUrl;
@XStreamAlias("pic_url")
private String picUrl;
@XStreamAlias("small_pic_url")
private String smallPicUrl;
@XStreamAlias("voucher_type")
private String voucherType;
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@Test
public void parseAlipayCreateReturn() {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"\n" +
"<alipay>\n" +
" <is_success>T</is_success>\n" +
" <request>\n" +
" <param name=\"extend_params\">{\"secondary_merchant_id\":\"A80001\",\"secondary_merchant_industry\":\"7011\",\"secondary_merchant_name\":\"Muku\",\"store_id\":\"BJ_ZZ_001\",\"store_name\":\"Muku in the Dreieichstrabe\"}</param>\n" +
" <param name=\"_input_charset\">UTF-8</param>\n" +
" <param name=\"subject\">Payment by QR-Code</param>\n" +
" <param name=\"sign\">784a5f3087aec1eb397ddd3f70280b48</param>\n" +
" <param name=\"notify_url\">http://www.yezhou.cc/notify/alipay_global_notify.php</param>\n" +
" <param name=\"product_code\">OVERSEAS_MBARCODE_PAY</param>\n" +
" <param name=\"out_trade_no\">20190327100628310</param>\n" +
" <param name=\"trans_currency\">THB</param>\n" +
" <param name=\"partner\">2088621924156138</param>\n" +
" <param name=\"service\">alipay.acquire.precreate</param>\n" +
" <param name=\"total_fee\">11.0</param>\n" +
" <param name=\"currency\">THB</param>\n" +
" <param name=\"sign_type\">MD5</param>\n" +
" <param name=\"timestamp\">1553652388309</param>\n" +
" </request>\n" +
" <response>\n" +
" <alipay>\n" +
" <big_pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=L</big_pic_url>\n" +
" <out_trade_no>20190327100628310</out_trade_no>\n" +
" <pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=M</pic_url>\n" +
" <qr_code>https://qr.alipay.com/bax04397fxtnhmvfaqkd0037</qr_code>\n" +
" <result_code>SUCCESS</result_code>\n" +
" <small_pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&amp;picSize=S</small_pic_url>\n" +
" <voucher_type>qrcode</voucher_type>\n" +
" </alipay>\n" +
" </response>\n" +
" <sign>6585d039857aa946e11f289cdf127002</sign>\n" +
" <sign_type>MD5</sign_type>\n" +
"</alipay>";
log.info(xml);
XStream xStream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_"))); //解决下划线问题
//com.thoughtworks.xstream.mapper.CannotResolveClassException: alipay
xStream.alias("alipay", AlipayCreateReturn.class);
//com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field
xStream.autodetectAnnotations(true); //自动检测注解
xStream.processAnnotations(AlipayCreateReturn.class); //应用Bean类的注解
AlipayCreateReturn alipayCreateReturn = (AlipayCreateReturn) xStream.fromXML(xml);
log.info(alipayCreateReturn.toString());
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :