JSON 反序列化时丢失属性

JSON loses attributes when deserializing

客户端向服务器传递一个JSON字符串,服务器将字符串反序列化为对象,如下所示:

    @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 ...
}

为什么?

如何使反序列化正常?

你的 VideoBean 是错误的(class 中的 originalFilename 与 JSON 中的 originalFileName,注意小写 n name)。使用以下一个:

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