【问题标题】:Reading JSON thru web service into POJOs annotated for Hibernate通过 Web 服务将 JSON 读取到为 Hibernate 注释的 POJO 中
【发布时间】:2023-03-04 16:55:01
【问题描述】:

我正在通过 Web 服务读取以下 json。有没有办法将 json 读入三个合适的 POJO? POJO 由 hibernate 生成,用于与数据库通信。

基本上我需要将person json 读入Person POJO,将pets json 读入一组Pet POJO,并将toy json 读入一组Toy POJO。

JSON

{
"person":{"first_name":"John", "last_name":"Smith"},
"pets":[{"species":"dog", "name":"Adama"}, {"species":"cat", "name":"Benton"} ],
"toys":[{"car":"corvet", "color":"black"}, {"action_figure":"hancock", "height":"1ft"} ]
}

网络服务

@Post
public Representation readForm(Representation representation) {
    try {
        Person aPerson = …
        Set<Pet>  petSet = …
        Set<Toy> toySet = ...

      ….

【问题讨论】:

    标签: json hibernate pojo restlet-2.0


    【解决方案1】:

    您可以使用 xStream 。您将必须创建一个将所有 3 种类型的对象作为属性的 VO。给它们各自的别名,您将在该 VO 中获得所有 3 种类型的对象。您只需调用它们的 getter 即可获取它们。

    【讨论】:

      猜你喜欢
      • 2013-10-17
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 2017-03-14
      • 2012-11-30
      • 1970-01-01
      相关资源
      最近更新 更多