[英]JAX-RS (Jersey) creates related object instead of null
problem is rather simple, but kinda hard to explain. 问题很简单,但是有点难以解释。
I have REST service like this: 我有这样的REST服务:
@Path("categories")
@RequestScoped
public class CategoryResource {
@Inject
CategoriesFacade dao;
@PUT
@Consumes("application/json")
public void putJson(Categories content) {
System.err.println(content.toString()); // <-- ?????
dao.edit(content);
}
// ....
}
And entity object like this: 和这样的实体对象:
@XmlRootElement
public class Categories implements Serializable {
@Id
@Column(name = "CATEGORY_ID", unique = true, nullable = false, precision = 5)
private Integer categoryId;
@Column(name = "NAME")
private String name;
@JoinColumn(name = "PARENT_ID", referencedColumnName = "CATEGORY_ID")
@ManyToOne
private Categories parentId;
// ....
}
Here is http request payload: 这是http请求有效负载:
PUT http://localhost:8080/api/categories
Data: {"categoryId":"7162","name":"test","parentId":null}
And here is Categories object i get in @PUT
request (see line marked with ????): 这是我在
@PUT
请求中获得的Categories对象(请参阅标有????的行):
Categories{
categoryId=7162,
name=test,
parentId=Categories{categoryId=null, name=null, parentId=null}
}
So as you can see here, parentId
is assigned with empty Categories
object, this way i'm getting ValidationException, because name
could not be null. 因此,正如您在此处看到的那样,为
parentId
分配了空的Categories
对象,这样我就收到了ValidationException,因为name
不能为null。
Any idea how to make Jersey to give me this kind of object, so null value for parentId will be fine: 任何想法如何使Jersey都可以给我这种对象,所以parentId的null值就可以了:
Categories{
categoryId=7162,
name=test,
parentId=null
}
Stringify your object on client site (for example, if client on JavaScropt, using method JSON.stringify
). 在客户端站点上对对象进行字符串化(例如,如果客户端在JavaScropt上,则使用
JSON.stringify
方法)。
Rest method make 休息法制作
@PUT
@Consumes("application/json")
public void putJson(String content) {
And convert json string to object (for eample, using google json) 并将json字符串转换为对象(例如,使用google json)
Categories cont = new com.google.gson.Gson().fromJson(content, Categories.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.