gson JSON在反序列化时丢失属性[已关闭]

iyfamqjs  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(184)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受答案。

这个问题是由一个打字错误或一个无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
12个月前关闭。
Improve this question
客户端将一个JSON字符串传递给服务器,服务器将该字符串反序列化为一个Object,如下所示:

@PostMapping("/web/UploadVideo")
    @ApiOperation("UploadVideo")
    public Result uploadVideoWB(HttpServletRequest request,
                                @RequestParam("sourceFile") MultipartFile sourceFile,
                                @RequestParam("videoBean") String videoBean) {
        Object object = request.getSession().getAttribute("UserId");
        if (object == null) {
            System.out.println("userId is null");
            return new Result(false, "UserId is null", null, null);
        } else {
            System.out.println("1. " + videoBean);
            VideoBean video = new Gson().fromJson(videoBean, VideoBean.class);
            video.setVideoUser((String) object);
            System.out.println("2. " + new Gson().toJson(video));
            return videoService.uploadVideo(sourceFile, new Gson().toJson(video));
        }
    }

控制台输出如下所示:格式化它们:

{
    "objectId": "e3da789b-c514-2a55-c5f8-1882ae4863c9", 
    "videoId": "3be5e2c3-edf5-66d9-3c93-e3b4d2cdc35c", 
    "sha1": "92e343d96ca29ddf3b631f52f39e771fabff1c97", 
    "duration": 0, 
    "fileCategory": "bbe0e1d4c9bc4e9390871a52cd641c42", 
    "originalFileName": "WeChat_20211124011655.mp4", 
    "sourceFileName": "e1d26875-cd88-423d-568b-86b2cfeee7fc.mp4", 
    "coverFileName": "db0bc7a4-f900-0483-13c6-5d0227d10e27.png", 
    "thumbnailFileName": "5a34eb29-eaa2-a507-baab-97951c06926c.png", 
    "blurFileName": "17dceb9c-0c88-dcd1-cb4d-96e3c4f9a814.png", 
    "createTime": 1637692690492, 
    "videoUser": "", 
    "videoStatus": 1
}

{
    "objectId": "e3da789b-c514-2a55-c5f8-1882ae4863c9", 
    "videoId": "3be5e2c3-edf5-66d9-3c93-e3b4d2cdc35c", 
    "sha1": "92e343d96ca29ddf3b631f52f39e771fabff1c97", 
    "duration": 0, 
    "fileCategory": "bbe0e1d4c9bc4e9390871a52cd641c42", 
    "sourceFileName": "e1d26875-cd88-423d-568b-86b2cfeee7fc.mp4", 
    "coverFileName": "db0bc7a4-f900-0483-13c6-5d0227d10e27.png", 
    "thumbnailFileName": "5a34eb29-eaa2-a507-baab-97951c06926c.png", 
    "blurFileName": "17dceb9c-0c88-dcd1-cb4d-96e3c4f9a814.png", 
    "videoUser": "84f6e0d8d94348ccab981e364661a419", 
    "videoStatus": 1, 
    "createTime": 1637692690492
}

您可以看到,originalFileName属性已消失!!!
VideoBean.class:

public class VideoBean {
    private String objectId;
    private String videoId;
    private String sha1;
    private long duration;
    private String fileCategory;
    private String originalFilename;
    private String sourceFileName;
    private String coverFileName;
    private String thumbnailFileName;
    private String blurFileName;
    private String videoUser;
    private int videoStatus;
    private long createTime;

    getter/setter ...
}

为什么?
我应该怎么做才能使反序列化正常?

6ljaweal

6ljaweal1#

您的VideoBean是错误的(您的类中的originalFilename与JSON中的originalFileName,请注意name中的小写n)。请使用以下代码:

public class VideoBean {
    private String objectId;
    private String videoId;
    private String sha1;
    private long duration;
    private String fileCategory;
    private String originalFileName;
    private String sourceFileName;
    private String coverFileName;
    private String thumbnailFileName;
    private String blurFileName;
    private String videoUser;
    private int videoStatus;
    private long createTime;

    getter/setter ...
}

相关问题