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

以支付宝API返回数据为例

返回xml

<?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&picSize=L</big_pic_url>
      <out_trade_no>20190327100628310</out_trade_no>
      <pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&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&picSize=S</small_pic_url>
      <voucher_type>qrcode</voucher_type>
    </alipay>
  </response>
  <sign>6585d039857aa946e11f289cdf127002</sign>
  <sign_type>MD5</sign_type>
</alipay>

映射Bean

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;
    }
}

测试

@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&picSize=L</big_pic_url>\n" +
            "      <out_trade_no>20190327100628310</out_trade_no>\n" +
            "      <pic_url>http://mobilecodec.alipaydev.com/show.htm?code=bax04397fxtnhmvfaqkd0037&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&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());
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/09/spring-boot-integrate-xstream-to-parse-multi-level-complex-data/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Spring Boot集成XStream解析多层级复杂数据
以支付宝API返回数据为例 返回xml <?xml version="1.0" encoding="utf-8"?> <alipay> <is_success>T</is_succe……
<<上一篇
下一篇>>
文章目录
关闭
目 录