@jsonignore仅用于某些特定端点

dkqlctbz  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(332)

在我的项目中,我一直在努力解决这个问题:是否可以使用注解 @JsonIgnore 仅当端点具有特定值时?
例如,我想在 endpoint.equals("") ,但不用于 endpoint.equals("yyyyyy"). 有3个类具有这些关系注解:
客户

@OneToMany(mappedBy = "ownerOfTheProduct")
    @JsonIgnore
    private List<Product> ownProducts = new ArrayList<>();

类别

@JsonIgnore
    @OneToMany(mappedBy = "category")
    private List<Product> products;

产品

@ManyToOne
    @JoinTable(name = "PRODUCT_CATEGORY", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "category_id"))
    private Category category;

    @ManyToOne
    @JoinTable(name = "CLIENT_PRODUCT", joinColumns = @JoinColumn(name = "product_id"), inverseJoinColumns = @JoinColumn(name = "client_id"))
    private Client ownerOfTheProduct;

关键是:
如果我不把 @JsonIgnore ,我得到一个stackoverflow错误,json进入循环并且不会停止。

"id": 1,
    "name": "Product name",
    "price": 20.0,
    "category": {
        "id": 1,
        "name": "Cleaning",
        "products": [
            {
                "id": 1,
                "name": "Product name",
                "price": 20.0,
                "category": {
                              ...

当我以不同的方式绘制Map @JsonIgnore 分为两类:客户机和产品,它的作品,循环没有更多的hapenning。但是,当我不得不使用其他端点时,哪些字段 products 以及 ownerOfTheProduct 需要通过api显示,因为 @JsonIgnore 已注解。
循环已解决

{
    "id": 1,
    "name": "Product name",
    "price": 20.0,
    "category": {
        "id": 1,
        "name": "Cleaning"
    },
    "ownOfTheProduct": {
        "id": 1,
        "name": "Edited",
        "cpf": "Edited",
        "email": "test",
        "password": "test"
    }
}

其他终结点不工作

{
    "id": 1,
    "name": "Edited",
    "cpf": "Edited",
    "email": "test",
    "password": "test"
}

我想要我已经Map的字段 @JsonIgnore (ownproducts)以这种方式出现在请求中:

{
    "id": 1,
    "name": "Edited",
    "cpf": "Edited",
    "email": "test",
    "password": "test"
    "ownProducts" [
           {
    "id": 1,
    "name": "Product name",
    "price": 20.0,
    "category": {
        "id": 1,
        "name": "Cleaning"
    },
                ]
}

有没有办法改变这种状况?总结一下,我只想用 @JsonIgnore 使用特定的端点,而不是api上的每个端点。
我希望yall能回答我的问题,这里是github上存储库的链接:https://github.com/reness0/spring-restapi-ecommerce

q3qa4bjr

q3qa4bjr1#

虽然使用基于注解的方法无法实现这一点(注解使其成为静态的),但是可以使用任何数据Map器库来实现这一点。基于api中的属性创建过滤器。orika库可用于:https://www.baeldung.com/orika-mapping

n1bvdmb6

n1bvdmb62#

你不能只使用 @JsonIgnore 但你可以用 @JsonView 以及 @JsonIdentityInfo 注解来自 com.fasterxml.jackson.core 工作原理:
你需要用接口定义类。例如:

public class SomeView {

    public interface  id {}

    public interface CoreData extends id {}

    public interface FullData extends CoreData {}

}

将实体字段标记为 @JsonView(<some interface.class>) ```
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonView(SomeView.id.class)
private Long id;

@Column(nullable = false)
@JsonView(SomeView.CoreData.class)
private String username;

@Column(nullable = false)
@JsonView(SomeView.FullData.class)
private String email;

}

用注解端点 `@JsonView(<some interface.class>)` ```
@GetMapping()
 @JsonView(SomeView.FullData.class)
 public User getUser() {
     return <get user entity somwhere>
 }

万一 @JsonView(SomeView.id.class) 您将得到以下json:

{
        id: <some id>
    }

万一 @JsonView(SomeView.CoreData.class) :

{
        id: <some id>,
        username: <some username>
    }

万一 @JsonView(SomeView.FullData.class) :

{
        id: <some id>,
        username: <some username>,
        email: <some email>
    }
``` `@JsonView` 也适用于嵌入对象,可以用多视图类注解一个字段- `@JsonView({SomeView.FullData.class, SomeOtherView.OtherData.class})` 关于循环json。用注解注解实体类

@JsonIdentityInfo(
property = "id",
generator = ObjectIdGenerators.PropertyGenerator.class
)

每次json序列化进入循环时,对象数据都将替换为对象id或其他实体字段,供您选择。
或者,您也可以使用dto类

相关问题