在POST / PUT期间,所有对象的单个自定义反序列化器作为其ID或嵌入的整个对象 [英] single custom deserializer for all objects as their ids or embedded whole objects during POST/PUT

查看:135
本文介绍了在POST / PUT期间,所有对象的单个自定义反序列化器作为其ID或嵌入的整个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @Entity 
公共产品{
@Id
public int id;

public String name;

@ManyToOne(cascade = {CascadeType.DETACH})
类别类别

@ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.DETACH})
设置<类别> secondaryCategories;


}

此实体:

  @Entity 
public Category {
@Id
public int id;

public String name;
}

我希望能够用json发送POST



来自客户端的{name:name,类别:2,secondaryCategories:[3,4,5]} / p>

并且能够反序列化如下:

  {name:名称,类别:{id:2},secondaryCategories:[{id:3},{id:4},{id:5}]} 

以防发送时间为

  {name:name,category :{id:2},secondaryCategories:[{id:3},{id:4},{id:5}]} 

我希望它仍能像现在一样工作



我需要什么样的注释和自定义反序列化器?希望反序列化器可以用于所有可能具有id作为属性的对象



谢谢!



编辑




解决方案

另一种方法是使用 @JsonCreator 工厂方法,如果你可以修改你的实体

 私有类产品{
@JsonProperty(category)
私有类别类别;

@JsonProperty(secondaryCategories)
private List< Category> secondaryCategories;
}


私有类别类别$
@JsonProperty(id)
private int id;

@JsonCreator
public static Category factory(int id){
Category p = new Category();
p.id = id;
//或某个db调用
返回p;
}
}

甚至这样的东西也应该起作用

 私有类别类别{
private int id;

public Category(){}

@JsonCreator
public Category(int id){
this.id = id;
}
}


@Entity
public Product {
   @Id
   public int id;

   public String name;

   @ManyToOne(cascade = {CascadeType.DETACH} )
   Category category

   @ManyToMany(fetch = FetchType.EAGER, cascade = {CascadeType.DETACH} )
   Set<Category> secondaryCategories;


}

and this entity:

@Entity
public Category {
   @Id
   public int id;

   public String name;
}

I would like to be able to send a POST with json

{ name: "name", category: 2, secondaryCategories: [3,4,5] } from client-side

and be able to be deserialized like:

{ name: "name", category: {id: 2 }, secondaryCategories: [{id: 3}, {id: 4}, {id: 5}] }

in case it was sent as

 { name: "name", category: {id: 2 }, secondaryCategories: [{id: 3}, {id: 4}, {id: 5}] }

I would like it to still work as now

what kind of annotation and custom deserializer I need? Hopefully the deserializer can work for all possible objects that have id as a property

Thanks!

Edit

解决方案

Another approach is to use @JsonCreator factory method if you can modify your Entity

private class Product {
    @JsonProperty("category")
    private Category category;

    @JsonProperty("secondaryCategories")
    private List<Category> secondaryCategories;
}


private class Category {
    @JsonProperty("id")
    private int id;

    @JsonCreator
    public static Category factory(int id){
        Category p = new Category();
        p.id = id;
        // or some db call 
        return p;
    }
}

Or even something like this should also work

private class Category {
    private int id;

    public Category() {}

    @JsonCreator
    public Category(int id) {
        this.id = id;
    }
}

这篇关于在POST / PUT期间,所有对象的单个自定义反序列化器作为其ID或嵌入的整个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆