Grails 2.4命名为JSON配置不起作用 [英] Grails 2.4 named configuration for JSON not working

查看:101
本文介绍了Grails 2.4命名为JSON配置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用指定的命名配置将对象呈现为JSON。我在做什么错了?



我在Bootstrap.groovy init方法中定义了一个命名的配置

  import com.appromocodes.Project 
import com.appromocodes.Promocode
import grails.converters.JSON
$ b $ class BootStrap {

def init = {servletContext - >


JSON.createNamedConfig('apiCheck',{
JSON.registerObjectMarshaller(Promocode){Promocode promocode - >
def map = [:]
map ['code'] = promocode.code $ b $ map ['allowedUses'] = promocode.allowedUses
map ['customInfo'] = promocode.customInfo

返回地图

})

}
def destroy = {
}
}
pre>

然后我有一个经典的控制器(不是REST,而是简单的控制器):

  import grails.converters.JSON 

class ApiV1Controller {

def apiV1Service

def check(){

log.info(check);
$ b $ def resultMap = apiV1Service.checkPromocode(params.projectKey,params.code)


if(resultMap.statusCode!= ResponseStatus.PROMOCODE_USED){
}

def p = Promocode.get(1)

JSON.use('apiCheck',{
渲染为JSON
})

}

}

检查操作的调用将仅输出在apiCheck命名配置中指定的三个属性,而不是我得到的所有bean属性以及metaClass属性class和id。



如果我没有指定一个命名的配置,那么JSON会正确呈现显示只有三个属性的bean。



有什么错?是否可以在非REST控制器中使用namedConfig?

解决方案

DefaultConverterConfiguration 默认配置作为参数传递给闭包。 该配置必须用于 registerObjectMarshaller 。因此,关闭必须按照以下方式执行(请注意关闭参数)。

  JSON.createNamedConfig('apiCheck', {config  - > 
config.registerObjectMarshaller(Promocode){Promocode promocode - >
def map = [:]
map ['code'] = promocode.code
map ['allowedUses'] = promocode.allowed使用
map ['customInfo'] = promocode.customInfo

返回地图
}
})

一个更简单,更清晰更实用的实现将是:

pre > JSON.createNamedConfig('apiCheck'){
it.registerObjectMarshaller(Promocode){Promocode promocode - >
[
code:promocode.code,
allowedUses:promocode.allowedUses,
customInfo:promocode.customInfo
]
}
}


I can't use a specified named config to render object as JSON. What i'm doing wrong?

I defined a named config in Bootstrap.groovy init method

import com.appromocodes.Project
import com.appromocodes.Promocode
import grails.converters.JSON

class BootStrap {

    def init = { servletContext ->


        JSON.createNamedConfig('apiCheck', {
            JSON.registerObjectMarshaller(Promocode) { Promocode promocode ->
                def map= [:]
                map['code'] = promocode.code
                map['allowedUses'] = promocode.allowedUses
                map['customInfo'] = promocode.customInfo

                return map              
              }
        })

    }
    def destroy = {
    }
}

Then i have a classic controller (not REST, but simple controller):

import grails.converters.JSON

class ApiV1Controller {

def apiV1Service

    def check() {

        log.info("check");

        def resultMap = apiV1Service.checkPromocode(params.projectKey, params.code)


        if (resultMap.statusCode != ResponseStatus.PROMOCODE_USED) {
        }

        def p = Promocode.get(1)

        JSON.use('apiCheck', {
            render p as JSON
        })

    }

}

I would expect that invocation of check action would output only the three properties specified in apiCheck named config, instead i get all the bean properties and also the metaClass properties "class" and "id".

If i don't specify a named config, then JSON renders correctly the bean showing only three properties.

What is wrong? Is it possible to use namedConfig also in non REST controllers?

解决方案

DefaultConverterConfiguration as JSON with default config is passed on to the closure as a parameter. That configuration has to be used to registerObjectMarshaller. So the closure has to be implemented as below (note the param to the closure).

JSON.createNamedConfig('apiCheck', { config ->
     config.registerObjectMarshaller(Promocode) { Promocode promocode ->
         def map= [:]
         map['code'] = promocode.code
         map['allowedUses'] = promocode.allowedUses
         map['customInfo'] = promocode.customInfo

         return map              
     }
})

An easier, clear and groovier implementation would be:

JSON.createNamedConfig( 'apiCheck' ) { 
     it.registerObjectMarshaller( Promocode ) { Promocode promocode ->
         [ 
             code        : promocode.code, 
             allowedUses : promocode.allowedUses,
             customInfo  : promocode.customInfo
         ]
     }
}

这篇关于Grails 2.4命名为JSON配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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