JSON converter in Grails
-
11-06-2021 - |
Pergunta
Consider a simple class as :-
class Person{
String name
String email
int age
String mobileNum
}
I want to selectively send 'id' & 'email' attributes as JSON response. When using:-
return theObject as JSON
the response has all attribute:value pair.
Solução
You can register your own JSON marshaller (at BootStrap.groovy
, for example), like:
JSON.registerObjectMarshaller(Person) { Person it ->
return [
id : it.id,
email : it.email
]
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow