【问题标题】:Receiving complex json object in Grails 2.3.3在 Grails 2.3.3 中接收复杂的 json 对象
【发布时间】:2013-12-12 14:04:36
【问题描述】:

我也有类似的问题:Grails get child domain objects

我将粘贴链接中的域类代码,以便帖子更具可读性:

class Parent{
   String name
   static hasMany = [childs:Child] 
   static constraints = {
   }
}

class Child{
   String name
   static belongsTo = [parent:Parent]
   static constraints={}
}

我已经为 Parrent 对象创建了自定义编组器,它会产生如下所示的内容。 我如何接收和保存这样一个复杂的 JSON 对象?(我想接收与 marshaller 为我生成的对象一样的相同对象)

[{"class":"project.Parent","id":1,"name":"name1",
  "childs":[{"class":"Child","id":1,"name":"childname1"},
            {"class":"Review","id":2,"name":"childname2"}
           ]
}]

如果我有 JSON 之外的所有参数,比如“名称”等,我的应用程序中的默认保存代码是:

def parrent = new Parrent("parr_name") 
parrent.save()
def child = new Child("child_name", parrent)
child.save()  

我必须单独发送 JSON 对象吗?还是我可以只接收复杂的 json 并拖出 args 地图?以这种方式实现显示 JSON 的编组器是否能够接收类似格式的编组器? 我正在使用 grails v2.3.3

【问题讨论】:

    标签: json rest grails grails-domain-class


    【解决方案1】:

    Grails 2.3.* 附带一个重新定义的数据绑定框架,您可以在其中将 JSON 有效负载(大多数情况下来自 POST 请求)直接绑定到域类。

    它的工作方式与您为响应 JSON 定制编组器相同。 CommandObjects 在早期版本的 Grails 中使用的方式同样可以使用。以你自己的例子:

    class Parent {
        String name
        static hasMany = [children:Child]
    }
    
    class Child {
        String name
        static belongsTo = [parent:Parent]
    }
    
    //In Controller
    class ParentController {
    
        //Directly binding the payload JSON to Parent domain class
        //Caveat here is if the corresponding Parent domain matching the id in the
        //payload is not found then "parent" parameter 
        //in the below action will be null 
        def save(Parent parent) {
            println "Parent Data $parent"
            println "Child Data $parent.children"
    
            render "Parent Data $parent and child data $parent.children"
    
            //or Do other stuff
        }
    }
    
    //Assuming you have parent and child already present
    class BootStrap {
        def init = { servletContext ->
            def parent = new Parent(name: "name1")
    
            def child1 = new Child(name: "childname1")
            def child2 = new Child(name: "childname2")
    
            parent.addToChildren(child1)
            parent.addToChildren(child2)
    
            parent.save(flush: true, failOnError: true)
        }
    }
    
    //and payload as
    POST http://localhost:8080/YourApp/parent/save
    Content-Type: application/json
    
    {"class":"project.Parent","id":1,"name":"name1",
      "childs":[{"class":"Child","id":1,"name":"childname1"},
                {"class":"Child","id":2,"name":"childname2"}
               ]
    }
    
    //curl command like
    curl -X POST 
         -H "Content-Type: application/json" 
         -d '{"class":"project.Parent","id":1,"name":"name1",
              "childs":[{"class":"Child","id":1,"name":"childname1"},
                        {"class":"Child","id":2,"name":"childname2"}
                       ]
             }' 
          http://localhost:8080/YourApp/parent/save
    

    您应该能够将 JSON 有效负载直接绑定到 Parent 域(带子域)。如果您使用 url 参数而不是 JSON,则同样适用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-02
      相关资源
      最近更新 更多