当前位置: 首页 > 工具软件 > fastJSON 2 > 使用案例 >

fastjson2 json字符串转对象字段赋值失败

夏侯野
2023-12-01

背景

最近在做一个微信小程序订阅服务通知的需求,用户订阅后,微信会有一个回调的请求,请求内容是XML,我需要把XML转为自定义对象处理。由于考虑到fastjson之前出现过漏洞,所以考虑使用最新的fastjson2,没想到字段赋值失败了……

代码

使用到的依赖以及使用到的fastjson版本

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.18</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
			<version>1.2.69</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.12</version>
        </dependency>
package com.xxx;

import lombok.Data;
import java.util.List;

/**
 * @author: Lewis
 * @date: 2022/9/6 16:07
 * @description: 微信回调消息封装
 */
@Data
public class WxCallbackMessageDto {

    /**
     * 小程序帐号ID
     */
    private String ToUserName;
    /**
     * 用户openid
     */
    private String FromUserName;
    /**
     * 时间戳
     */
    private Long CreateTime;
    /**
     * 消息类型
     */
    private String MsgType;
    /**
     * 事件类型
     */
    private String Event;

    private SubscribeMsgPopupEvent SubscribeMsgPopupEvent;

    @Data
    public static class SubscribeMsgPopupEvent {
        private List<Template> List;
    }

    @Data
    public static class Template {
        private String TemplateId;
        private String SubscribeStatusString;
        private String PopupScene;
    }

}
package com.xxx;

import org.junit.Test;

/**
 * @author: Lewis
 * @date: 2022/2/9 16:13
 * @description: 测试
 */
public class CommonTest {

    @Test
    public void xmlTest() {
        String xml = "<xml><ToUserName><![CDATA[gh_123456789abc]]></ToUserName><FromUserName><![CDATA[otFpruAK8D-E6EfStSYonYSBZ8_4]]></FromUserName><CreateTime>1610969440</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe_msg_popup_event]]></Event><SubscribeMsgPopupEvent><List><TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId><SubscribeStatusString><![CDATA[accept]]></SubscribeStatusString></List><List><TemplateId><![CDATA[VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc]]></TemplateId><SubscribeStatusString><![CDATA[accept]]></SubscribeStatusString></List></SubscribeMsgPopupEvent></xml>";
        cn.hutool.json.JSONObject xmlJsonObject = cn.hutool.json.XML.toJSONObject(xml).getJSONObject("xml");
        WxCallbackMessageDto wxCallbackMessageDto = com.alibaba.fastjson.JSON.parseObject(xmlJsonObject.toString(), WxCallbackMessageDto.class);
        WxCallbackMessageDto wxCallbackMessageDto2 = com.alibaba.fastjson2.JSON.parseObject(xmlJsonObject.toString(), WxCallbackMessageDto.class);
        System.out.println("wxCallbackMessageDto=" + JSON.toJSONString(wxCallbackMessageDto));
        System.out.println("wxCallbackMessageDto2=" + JSON.toJSONString(wxCallbackMessageDto2));
    }

}

执行结果

wxCallbackMessageDto={"createTime":1610969440,"event":"subscribe_msg_popup_event","fromUserName":"otFpruAK8D-E6EfStSYonYSBZ8_4","msgType":"event","subscribeMsgPopupEvent":{"list":[{"subscribeStatusString":"accept","templateId":"VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc"},{"subscribeStatusString":"accept","templateId":"VRR0UEO9VJOLs0MHlU0OilqX6MVFDwH3_3gz3Oc0NIc"}]},"toUserName":"gh_123456789abc"}
wxCallbackMessageDto2={}

结论

fastjson2不兼容字段非正常驼峰命名的情况,最后采用了fastjson较高一点的版本。

 类似资料: