JACKSON REST API仅在ManyToOne中添加具有其ID的实体字段 [英] JACKSON REST API Add Entity Field with its id Only in ManyToOne
本文介绍了JACKSON REST API仅在ManyToOne中添加具有其ID的实体字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对Spring Rest API有点陌生.我有两个具有单向多对一关系的实体.
I'm kinda new with spring rest api. I have two entities that have unidirectional many to one relationship.
@Entity
public class Users{
@Id @Column(name = "user_id") @JsonProperty("userId")
private int id;
@ManyToOne @JoinColumn("city_id")
private City city;
// other fields, getters, setters
}
@Entity
public class City{
@Id @Column(name = "city_id") @JsonProperty("cityId")
private int id;
private String name;
// other fields, getters, setters
}
假设我在城市表中已经有一些城市.当我想使用http post方法添加城市ID为2的新用户时,我必须执行以下操作:
Suppose that I already have some cities in city table. When I want to add new user with city id 2 using http post method, I had to do something like this:
{
"userId": 1,
"city": {
"cityId": 2
}
}
如您所见,我必须先将cityId
分组到city
实体中.不进行分组怎么办?像这样:
As you can see I had to group cityId
inside city
entity first. How can I do it without grouping it? like this :
{
"userId": 1,
"cityId": 2
}
推荐答案
查看全文