当前位置: 首页 > 知识库问答 >
问题:

Hibernate JPA Spring Boot忽略特定请求的JSON属性

况繁
2023-03-14
@Entity 
public class ProductItem {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Long id;
    @ManyToOne
    private Organization org;
    @ManyToOne
    private Supplier supplier;
    private String barCode;
    private String description;
    private String name;
    private Integer stock;
    private Integer caseQty;
    private BigDecimal caseCost;
    @ManyToMany(cascade=CascadeType.ALL)
    private List<Note> notes;
    @CreationTimestamp
    private LocalDateTime createdOn;
    @UpdateTimestamp
    private LocalDateTime updatedOn;
@Entity 
public class Supplier {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    private Long id;
    private String name;
    @ManyToMany(cascade=CascadeType.ALL)
    private List<Note> notes;
    @ManyToOne(cascade=CascadeType.ALL)
    private Address address;
    @ManyToOne
    private Organization org;
{
    "id": 1,
    "org": {
        "id": 1,
        "name": "Test",
        "address": {
             "name": "Test"
            },
        "website": null,
        "phone": null
    },
    "supplier": {
        "id": 1,
        "name": "Sams Club",
        "notes": [],
        "address": {
         "name": "Test"
        },
        "org": {
            "id": 1,
            "name": "Test",
            "address": {
             "name": "Test"
            },
            "website": null,
            "phone": null
        }
    },
    "barCode": "UPC134",
    "description": "This is description. This is description. This is description. This is description. ",
    "name": "Sunchips",
    "stock": 10,
    "caseQty": 34,
    "caseCost": 29.99,
    "notes": [
        {
            "id": 1,
            "title": "Product Item Created",
            "description": "Note created by user X on 12/16/2019 11:00PM"
        },
        {
            "id": 2,
            "title": "Product Item Updated",
            "description": "Product ITem updated stock by user X on 12/16/2019 11:00PM"
        }
    ],
    "createdOn": "2019-02-28T13:44:39",
    "updatedOn": "2019-02-28T13:44:39"
}
{
    "id": 1,
    "org": {
        "id": 1,
        "name": "Test",
        "address": null,
        "website": null,
        "phone": null
    },
    "supplier": {
        "id": 1,
        "name": "Sams Club",
    },
    "barCode": "UPC134",
    "description": "This is description. This is description. This is description. This is description. ",
    "name": "Sunchips",
    "stock": 10,
    "caseQty": 34,
    "caseCost": 29.99,
    "notes": [
        {
            "id": 1,
            "title": "Product Item Created",
            "description": "Note created by user X on 12/16/2019 11:00PM"
        },
        {
            "id": 2,
            "title": "Product Item Updated",
            "description": "Product ITem updated stock by user X on 12/16/2019 11:00PM"
        }
    ],
    "createdOn": "2019-02-28T13:44:39",
    "updatedOn": "2019-02-28T13:44:39"
}
 {
        "id": 1,
        "name": "Sams Club",
        "notes": [],
        "address": {
         "name": "Test"
        },
        "org": {
            "id": 1,
            "name": "Test",
            "address": null,
            "website": null,
            "phone": null
        }

共有1个答案

俞新翰
2023-03-14

您可以使用Jackson Json视图来控制某些控制器方法的序列化/反序列化。简要示例

查看定义:

public class Views {
    public static class BriefSupplier {
    }

    public static class FullSupplier extends BriefSupplier {
    }
}

供应商:

@Entity 
public class Supplier {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @JsonView(Views.BriefSupplier.class)
    private Long id;
    @JsonView(Views.BriefSupplier.class)
    private String name;
    @ManyToMany(cascade=CascadeType.ALL)
    @JsonView(Views.FullSupplier.class)
    private List<Note> notes;
    @ManyToOne(cascade=CascadeType.ALL)
    @JsonView(Views.FullSupplier.class)
    private Address address;
    @ManyToOne
    @JsonView(Views.FullSupplier.class)
    private Organization org;
}
@JsonView(Views.BriefSupplier.class)
@RequestMapping("/product/{productId}")
public Product getProduct(@PathVariable int productId) { ... }

@JsonView(Views.FullSupplier.class)
@RequestMapping("/supplier/{supplierId}")
public Supplier getSupplier(@PathVariable int supplierId) { ... }
 类似资料:
  • 我试图爬过craiglist的论坛类别。组织机构(https://forums.craigslist.org/).我的蜘蛛: 我通过错误回调收到以下消息: [失败实例:Traceback:: /usr/local/lib/python2.7/site-packages/twisted/internet/defer.py: 455:回调 /usr/local/lib/python2.7/site-p

  • 问题内容: 我想比较两个JSON字符串,它们是一个巨大的层次结构,并且想知道它们的值在哪里不同。但是某些值是在运行时生成的,并且是动态的。我想从比较中忽略那些特定的节点。 我目前正在使用来自 org.SkyScreamer的JSONAssert 进行比较。它为我提供了不错的控制台输出,但没有忽略任何属性。 对于前。 现在这是动态的,应该忽略。就像是 如果有人建议使用JSONAssert解决方案,那

  • 我有以下课程- 我如何通过使用Jackson JSON API动态地排除字段序列化?

  • 问题内容: 我正在开发一个使用Spring-boot,关系数据库和Elasticsearch的应用程序。 我在代码的2个不同位置使用JSON序列化: 在REST API的响应中。 当代码与Elasticsearch交互时。 我在Elasticsearch中需要一些属性,但我想向应用程序用户隐藏(例如,来自关系数据库的内部ID)。 这是一个实体的例子: 问题 :当对象持久化在Elasticsearc

  • 使用ESLint是否可以忽略整个目录的一个特定规则? 在我的例子中,我想忽略为一个名为的目录

  • 有几种方法可以忽略mapstruct中未映射的目标属性。 对于特定方法,我们可以列出要忽略的所有属性: 是否有一种方法可以混合这些方法并忽略方法级别的所有属性,而无需明确列出所有属性?