Grails(15)How to Customized Marshaller

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值