JSON Marshaller控制器中的每个动作(Grails) [英] JSON Marshaller for each action in controller (Grails)

查看:211
本文介绍了JSON Marshaller控制器中的每个动作(Grails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在grails中,如何让控制器中的每个动作拥有JSON.registerObjectMarshaller。



这里是一个例子

我的用户域对象:

 字符串用户名
字符串empId
String attendanceID
字符串密码
字符串名字

在我的控制器:

  def myaction1(){
def user = User.getAll()
// XXX在这里我想返回只是用户名和empId
呈现用户作为JSON

$ b $ def myaction2(){
def user = User.getAll()
// XXX这里我想返回用户名和名字
将用户作为JSON
}


Map 您的数据,问题仍然有效。



您如何注册自定义命名marshallers?

p>

通常你会在 grails-app / conf / BootStrap.groovy (或者一个新文件 grails-app / conf / CustomMarshallersBootStrap.groovy 如果你想保持干净)。例如:

$ p $ // Bootstrap.groovy
导入grails.converters.JSON
import com.example.User
$ b $ class BootStrap {
def init = {servletContext - >
JSON.createNamedConfig(userEmployeeView,{
JSON.registerObjectMarshaller(User){User o - >
return [
username:o.username,
empId :o.empId
]
}
})
JSON.createNamedConfig(userOtherView,{
JSON.registerObjectMarshaller(User){User o - >
return [
username:o.username,
firstName:o.firstName
]
}
})
}
def这将注册两个名为marshallers,你可以在你的系统中使用它控制器是这样的:

  // UserController.groovy 
包com.example
导入grails .converters.JSON
$ b $ class UserController {
def action1(){
def users = User.getAll()
JSON.use(userEmployeeView){
将用户呈现为JSON
}
}

def action2(){
def users = User.getAll()
JSON.use(userOtherView){
将用户呈现为JSON
}
}
}


$ b

上面的用法叫做marshlers,它允许你控制哪个JSON表示(实际上只是一个 Map

希望这会有所帮助,并且原谅我在写我的头顶时出现的任何输入错误。

p>

In grails how to have JSON.registerObjectMarshaller for each action in controller.

Here is an example

My User domain object:

String username
String empId
String attendanceID
String password
String firstName

in my controller:

def myaction1() {
    def user=User.getAll()
    // XXX here i want to return just username and empId
    render user as JSON
}

def myaction2() {
    def user=User.getAll()
    // XXX here i want to return just username and firstName
    render user as JSON
}

解决方案

While it may be a bit overkill for such as simple domain, and you could likely get away with just returning a Map of your data, the question is still valid.

How do you register custom named marshallers?

Typically you will do this inside your grails-app/conf/BootStrap.groovy (or a new file grails-app/conf/CustomMarshallersBootStrap.groovy if you want to keep things clean). An example of this might look like this:

// Bootstrap.groovy
import grails.converters.JSON
import com.example.User

class BootStrap {
  def init = { servletContext ->
    JSON.createNamedConfig("userEmployeeView", {
      JSON.registerObjectMarshaller(User) { User o ->
        return [
          username: o.username,
          empId: o.empId
        ]
      }
    })
    JSON.createNamedConfig("userOtherView", {
      JSON.registerObjectMarshaller(User) { User o ->
        return [
          username: o.username,
          firstName: o.firstName
        ]
      }
    })
  }
  def destroy = { }
}

This will register two named marshallers which you can use in your controller(s) like this:

// UserController.groovy
package com.example
import grails.converters.JSON

class UserController {
  def action1() {
    def users = User.getAll()
    JSON.use("userEmployeeView") {
      render users as JSON
    }
  }

  def action2() {
    def users = User.getAll()
    JSON.use("userOtherView") {
      render users as JSON
    }
  }
}

The above uses named marshllers which allows you to control which JSON representation (actually just a Map) will be used when creating the final JSON output.

Hope this helps, and forgive any typos as I wrote this off the top of my head.

这篇关于JSON Marshaller控制器中的每个动作(Grails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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