- 浏览: 2552109 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Grails(15)How to Customized Marshaller
SOAPUI eclipse plugin URL
http://www.soapui.org/eclipse/update
Use the POSTMAN in chrome to test the REST API
We can create the Marshaller class especially for one class
package com.sillycat.project.util.marshaller
import grails.converters.JSON
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller
import org.codehaus.groovy.grails.web.json.JSONWriter
import com.sillycat.project.Store
import com.sillycat.project.util.Util
class StoreJSONMarshaller implements ObjectMarshaller<JSON> {
publicboolean supports(Object object) {
if(object instanceof Store){
return true;
}
return false;
}
publicvoid marshalObject(Object object, JSON converter)
throws ConverterException {
def store = (Store)object
JSONWriter writer = converter.getWriter();
writer.object();
def streetAddress = (store.address2 == null || store.address2.equals("")) ? store.address1 : store.address1 + ' ' + store.address2
writer.key("address")
writer.value([
'state':store.state,
'country': store.country,
'streetAddress': streetAddress,
city: store.city,
postalCode: store.zip
])
writer.key("id")
writer.value(store.getId())
writer.key("name")
writer.value(store.storeName)
writer.key("status")
writer.value(store.enabled?'ENABLED':'DISABLED')
writer.key("retailerStoreId")
writer.value(store.storeCode)
writer.key("geofences")
def latitude = null;
def longitude = null;
if(store.geoFence != null && store.geoFence.center != null){
latitude = store.geoFence.center.latitude;
}
if(store.geoFence != null && store.geoFence.center != null){
longitude = store.geoFence.center.longitude
}
writer.value([
name: store.storeName,
id: store.geoFence.id,
type: "STORE",
latitude: latitude,
longitude: longitude,
radius: store.geoFence.radius,
ssid : store.ssid,
bssid: store.bssid
])
def brandCode = null
//if(Util.getCurrentBrand() != null){
//brandCode = Util.getCurrentBrand().getCode()
//}
//writer.key("brandCode")
//writer.value(brandCode)
writer.key("timezone")
writer.value(store.timeZone == null ? null : store.timeZone.getID())
writer.endObject();
}
}
Here comes the test class:
@Test
publicvoid testGetSuccess() {
List<Store> stores = [
new Store(storeCode:"TS1",storeName:"Test Store 1",enabled:true),
new Store(storeCode:"TS2",storeName:"Test Store 2",enabled:true),
new Store(storeCode:"TS3",storeName:"Test Store 3",enabled:false)]
mockDomain(Store, stores)
JSON.registerObjectMarshaller(new StoreJSONMarshaller(), 1)
registerMetaClass(Util)
def controller = new StoreController()
controller.params.id = 1
controller.apiGet()
assertEquals(controller.response.status, 200)
//System.out.println(controller.response.status)
//System.out.println(controller.response.contentAsString)
def actualStore = JSON.parse(controller.response.contentAsString)
assertNotNull(actualStore)
assertEquals("Test Store 1",actualStore.name)
}
In the configuration file, BootStrap.groovy
bootstrapService.registerCustomJSONMarshallers()
And here is the class BootstrapService.groovy
import grails.converters.JSON
import com.sillycat.project.util.marshaller.StoreJSONMarshaller
class BootstrapService {
static transactional = true
void registerCustomJSONMarshallers() {
JSON.registerObjectMarshaller(new StoreJSONMarshaller(), 1)
}
}
References:
http://jwicz.wordpress.com/2011/07/11/grails-custom-xml-marshaller/
http://manbuildswebsite.com/2010/02/15/rendering-json-in-grails-part-3-customise-your-json-with-object-marshallers/
http://manbuildswebsite.com/2010/02/08/rendering-json-in-grails-part-2-plain-old-groovy-objects-and-domain-objects/
http://www.soapui.org/eclipse/update
SOAPUI eclipse plugin URL
http://www.soapui.org/eclipse/update
Use the POSTMAN in chrome to test the REST API
We can create the Marshaller class especially for one class
package com.sillycat.project.util.marshaller
import grails.converters.JSON
import org.codehaus.groovy.grails.web.converters.exceptions.ConverterException
import org.codehaus.groovy.grails.web.converters.marshaller.ObjectMarshaller
import org.codehaus.groovy.grails.web.json.JSONWriter
import com.sillycat.project.Store
import com.sillycat.project.util.Util
class StoreJSONMarshaller implements ObjectMarshaller<JSON> {
publicboolean supports(Object object) {
if(object instanceof Store){
return true;
}
return false;
}
publicvoid marshalObject(Object object, JSON converter)
throws ConverterException {
def store = (Store)object
JSONWriter writer = converter.getWriter();
writer.object();
def streetAddress = (store.address2 == null || store.address2.equals("")) ? store.address1 : store.address1 + ' ' + store.address2
writer.key("address")
writer.value([
'state':store.state,
'country': store.country,
'streetAddress': streetAddress,
city: store.city,
postalCode: store.zip
])
writer.key("id")
writer.value(store.getId())
writer.key("name")
writer.value(store.storeName)
writer.key("status")
writer.value(store.enabled?'ENABLED':'DISABLED')
writer.key("retailerStoreId")
writer.value(store.storeCode)
writer.key("geofences")
def latitude = null;
def longitude = null;
if(store.geoFence != null && store.geoFence.center != null){
latitude = store.geoFence.center.latitude;
}
if(store.geoFence != null && store.geoFence.center != null){
longitude = store.geoFence.center.longitude
}
writer.value([
name: store.storeName,
id: store.geoFence.id,
type: "STORE",
latitude: latitude,
longitude: longitude,
radius: store.geoFence.radius,
ssid : store.ssid,
bssid: store.bssid
])
def brandCode = null
//if(Util.getCurrentBrand() != null){
//brandCode = Util.getCurrentBrand().getCode()
//}
//writer.key("brandCode")
//writer.value(brandCode)
writer.key("timezone")
writer.value(store.timeZone == null ? null : store.timeZone.getID())
writer.endObject();
}
}
Here comes the test class:
@Test
publicvoid testGetSuccess() {
List<Store> stores = [
new Store(storeCode:"TS1",storeName:"Test Store 1",enabled:true),
new Store(storeCode:"TS2",storeName:"Test Store 2",enabled:true),
new Store(storeCode:"TS3",storeName:"Test Store 3",enabled:false)]
mockDomain(Store, stores)
JSON.registerObjectMarshaller(new StoreJSONMarshaller(), 1)
registerMetaClass(Util)
def controller = new StoreController()
controller.params.id = 1
controller.apiGet()
assertEquals(controller.response.status, 200)
//System.out.println(controller.response.status)
//System.out.println(controller.response.contentAsString)
def actualStore = JSON.parse(controller.response.contentAsString)
assertNotNull(actualStore)
assertEquals("Test Store 1",actualStore.name)
}
In the configuration file, BootStrap.groovy
bootstrapService.registerCustomJSONMarshallers()
And here is the class BootstrapService.groovy
import grails.converters.JSON
import com.sillycat.project.util.marshaller.StoreJSONMarshaller
class BootstrapService {
static transactional = true
void registerCustomJSONMarshallers() {
JSON.registerObjectMarshaller(new StoreJSONMarshaller(), 1)
}
}
References:
http://jwicz.wordpress.com/2011/07/11/grails-custom-xml-marshaller/
http://manbuildswebsite.com/2010/02/15/rendering-json-in-grails-part-3-customise-your-json-with-object-marshallers/
http://manbuildswebsite.com/2010/02/08/rendering-json-in-grails-part-2-plain-old-groovy-objects-and-domain-objects/
http://www.soapui.org/eclipse/update
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 451GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 573NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 370Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
《Grails 2 的终极指南》是一本深入探讨Grails框架精髓的专业书籍,该书以英文撰写,旨在为读者提供全面、深入的Grails框架学习资料。Grails框架基于Groovy语言,是一种高度动态、敏捷的Java应用开发框架,它简化了...
《The Definitive Guide to Grails 2》是Grails框架深入学习的重要参考资料,由业界专家撰写,旨在为开发者提供全面、详尽的Grails 2技术指导。这本书结合了理论与实践,不仅介绍了Grails的基本概念,还涵盖了高级...
**Grails 框架详解** Grails 是一个基于 Groovy 语言的开源Web应用程序框架,它构建在Java平台之上,旨在简化开发过程并提高生产力。Grails 的设计深受Ruby on Rails的影响,提供了MVC(模型-视图-控制器)架构模式...
《Grails权威指南》是一本全面深入探讨Grails框架的专著,旨在帮助读者掌握这一强大的Web开发工具。Grails是一种基于Groovy语言的开源框架,它为构建现代、高效的应用程序提供了简洁高效的解决方案。本指南针对不同...
【Grails项目搭建详解】 Grails是一个基于Groovy语言的开源Web应用框架,它简化了开发过程,尤其适合快速构建动态网站。在Eclipse中搭建Grails项目可能相对复杂,但通过以下步骤,即使是初学者也能顺利进行。 1. *...
《Grails用户手册》 Grails,作为一个基于Groovy语言的开源Web应用框架,深受开发者喜爱,它简化了Java开发的复杂性,提供了强大的MVC(Model-View-Controller)架构,以及丰富的插件系统。这份用户手册将帮助你...
对于Grails开发,我们需要的是Eclipse中的Grails插件,它能够提供对Grails项目的创建、运行、调试等一系列功能。 **Grails**是基于Groovy语言的全栈式Web开发框架,它借鉴了Ruby on Rails的设计理念,提供了快速...
Grails是一个基于Groovy语言的全栈框架,它遵循约定优于配置的原则,并且紧密集成Spring和Hibernate等流行的Java库,简化了开发流程。Grails在IT行业中尤其受到重视,因为它能够帮助开发者快速搭建并部署基于MVC模式...
本书《Beginning Groovy and Grails, From Novice to Professional》由Christopher M. Judd、Joseph Faisal Nusairat 和 James Shingler共同编写,并得到了Grails项目负责人Graeme Rocher的前言推荐。本书主要面向...
Grails home is set to: D:\D\MY_DEV\grails Base Directory: D:\Temp\grails_apps Environment set to production Note: No plugin scripts found Running script D:\D\MY_DEV\grails\scripts\CreateApp....
《Grails 中文参考手册》是一本全面介绍Grails框架的指南,旨在帮助开发者快速上手并深入理解Grails的各个核心概念和技术。Grails是一个基于Groovy语言的开源Web应用框架,它提供了高效的开发环境和强大的功能,使得...
### Grails 快速开发 Web 应用程序 #### 一、Grails 概述 Grails 是一种基于 Groovy 的开源应用框架,用于简化 Web 应用程序的开发过程。它采用约定优于配置的原则,这使得开发者可以更快地创建功能丰富的 Web ...
在 Grails 中,我们可以使用 belongsTo 和 hasMany 两个关键字来定义域关系。belongsTo 用于定义一个域对象所属的其他域对象,而 hasMany 用于定义一个域对象拥有的多个其他域对象的引用。 二、域类设计 在设计域...
《Grails 2.4.4 框架深度解析》 Grails 2.4.4 是一个基于Java的开源Web应用框架,它利用Groovy语言的强大特性,为开发者提供了一种高效、灵活的开发环境。这个压缩包“grails-2.4.4.zip”包含了完整的Grails 2.4.4...
**Grails 概述** Grails 是一个基于 Groovy 语言的开源 web 应用程序框架,它构建在 Java 平台上,旨在提高开发效率,简化常见 Web 开发任务。Grails 遵循 Model-View-Controller (MVC) 架构模式,允许开发者快速...
压缩包内的“one2many”可能是一个示例或教程,涉及到一对一(one-to-one)、一对多(one-to-many)或多对一(many-to-one)的关系映射。在数据库设计中,这些关系是常见的实体间关联。在Grails与GORM中,处理这种...