我的JSON序列化中的属性来自哪里

我的JSON序列化中的属性来自哪里

本文介绍了“目标"在哪里?我的JSON序列化中的属性来自哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MongoDB作为我的bean的存储库的spring boot服务.我添加了一个功能,可以以JSON格式下载bean并将其上传到另一个系统上(或者只是进行文件备份).

I have a spring boot service using MongoDB as a repository for my beans. I added a feature to download a bean in JSON format and upload it on another system (or simply to have a file backup).

我正在将ObjectMapperwriteValueAsString方法一起使用.所有这些都按预期工作,除了不属于我的bean的其他属性.

I am using the ObjectMapper with the writeValueAsString method. This all works as expected, except that there are additional properties which aren't part of my bean.

使用@DBRef定义的所有属性(因此指向MongoDB中的其他bean)都具有target属性,其中包含与序列化的bean完全相同的属性.例如:我跟踪通过GUI创建bean的用户:

All properties that are defined with @DBRef thus pointing to other beans in the MongoDB have a target property containing the exact same serialized bean. For ex: I keep track of the user that created the bean through a GUI:

{
    createdBy: {
        id: "5bb743feacbd6505304c025e",
        username: "admin",
        target: {
            id: "5bb743feacbd6505304c025e",
            username: "admin"
        }
    }
}

target的来源,有没有办法在JSON中消除它?

Where does this target come from and is there a way to get rid of it in the JSON?

推荐答案

来自

选项1:要忽略它们,您只需添加 @JsonIgnoreProperties(value = { "target" })在课堂上

Option1: To ignore them you just have to add @JsonIgnoreProperties(value = { "target" }) on class level

@Document(collection = "song")
@JsonIgnoreProperties(value = { "target" })
public class Song {
 ...
}

选项2:使集合不懒惰

选项3:如 Spring Data Mongo +所述,创建自己的DBRef序列化器延迟加载+ REST杰克逊

这篇关于“目标"在哪里?我的JSON序列化中的属性来自哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:22