您可以使用https://github.com/google/gson序列化/反序列化類,並設置字段命名策略:
Gson gson = new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create()
這意味着你應該把你的請求/響應機構爲文本,而不是依靠Grails提供的automagic json反序列化。
另一個選項,至少對於您的響應,是註冊一個自定義的JSON編組器。你可以在bootstrap.groovy中做到這一點。在我的示例代碼中,我已經實現了一個標記接口,以便可以明確提名應該作爲蛇情況序列化的對象。
interface SnakeCase {
}
域對象要導出蛇的情況下實現此接口
class Person implements SnakeCase {
String surname
String firstName
String middleNames
String someOtherField
static constraints = {
}
}
然後BootStrap.groovy中的自定義JSON編組註冊
class BootStrap {
def grailsApplication
def init = { servletContext ->
JSON.registerObjectMarshaller(SnakeCase, { SnakeCase o ->
def result = [:]
if (grailsApplication.isDomainClass(o.class)) {
o.domainClass.persistentProperties.each { GrailsDomainClassProperty p ->
def name = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, p.name)
result[name] = o[p.name]
}
} else {
o.properties.each {
def name = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, it.key as String)
result[name] = it.value
}
}
return result
})
}
def destroy = {
}
}
注意區分是由Grails領域類或常規POGO類之間。您還可以添加黑/白名單機制,以更好地控制哪些屬性被序列化。本例使用番石榴CaseFormat實用程序來執行實際的蛇殼
一旦編組已被註冊,它只是一個在你的控制器方法使用JSON格式的事
def ajaxListPeople() {
def list = Person.list();
render(list as JSON)
}