Grails 2.4命名为JSON配置不起作用

编程入门 行业动态 更新时间:2024-10-23 12:35:33
本文介绍了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配置不起作用

本文发布于:2023-11-01 23:13:31,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命名为   不起作用   Grails   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!