I am working with grails 2.5.0.
I want to render src/groovy
class as JSON
and custom JSON
. If I perform same with domain classes, its working fine for me. I want same thing with src/groovy classes.
For rendering data with json and custom json I followed this link.
I tried to these thing. Create pogo class in src/groovy.
class UserDTO {
String username
String lastName
String firstName
String email
}
Created a controller with restful feature.
import grails.converters.JSON
import grails.rest.RestfulController
class RenderDTOController extends RestfulController {
static responseFormats = ['json','pogoshort','pogodetails']
RenderDTOController() {
super(UserDTO)
}
def index() {
respond new UserDTO(username: "vivek", firstName: "Vivek", lastName: "Yadav")
}
}
Made an entry on urlMapping.groovy.
"/vivek"(resources:'renderDTO')
Registered new mime type for custom renderer in resources.groovy.
userPogoDetailsRenderer(JsonRenderer, UserDTO) {
mimeTypes = [new MimeType('application/vnd.grails.rest.sample.pogodetails+json', 'pogodetails')]
namedConfiguration = 'pogodetails'
}
output.
userPogoShortRenderer(JsonRenderer, UserDTO) {
mimeTypes = [new MimeType('application/vnd.grails.rest.sample.pogoshort+json', 'pogoshort')]
namedConfiguration = 'pogoshort'
}
userPogoRenderer(JsonRenderer, UserDTO) {
mimeTypes = [new MimeType('application/json', 'json')]
}
Added new mime type in mime type.
grails.mime.types = [ // the first one is the default format
all : '*/*', // 'all' maps to '*' or the first available format in withFormat
atom : 'application/atom+xml',
css : 'text/css',
csv : 'text/csv',
form : 'application/x-www-form-urlencoded',
html : ['text/html', 'application/xhtml+xml'],
js : 'text/javascript',
json : ['application/json', 'text/json'],
multipartForm: 'multipart/form-data',
rss : 'application/rss+xml',
text : 'text/plain',
hal : ['application/hal+json', 'application/hal+xml'],
xml : ['text/xml', 'application/xml'],
pogodetails : ['application/vnd.grails.rest.sample.pogodetails+json', 'application/json'],
pogoshort : ['application/vnd.grails.rest.sample.pogoshort+json', 'application/json']
]
Added custom marshaller in Bootstrap.groovy.
JSON.createNamedConfig("pogodetails") {
println "pogodetails renderer in bootstrap"
it.registerObjectMarshaller(UserDTO) { UserDTO user ->
final String fullname = [user.firstName, user.lastName].join(' ')
final userMap = [
lastName : user.lastName,
username: user.username,
email : user.email,
]
if (fullname) {
userMap.fullname = fullname
}
userMap
}
}
JSON.createNamedConfig('pogoshort') {
println "pogoshort renderer in bootstrap"
it.registerObjectMarshaller(UserDTO) { UserDTO user ->
println "Coming here in pogoshort renderer"
final userMap = [
lastName : user.lastName,
username: user.username
]
userMap
}
}
While I am running, I am getting exception.
URI
/grails-rest-2.5.0/vivek
Class
org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
Cannot cast object 'com.vivek.UserDTO@12c671a2' with class 'com.vivek.UserDTO' to class 'grails.converters.JSON'
If I change in controller like this.
respond new UserDTO(username: "vivek", firstName: "Vivek", lastName: "Yadav").encodeAsJSON()
I am able to convert it into JSON format. Also, If user this syntax.
JSON.use("pogodetails")
respond new UserDTO(username: "vivek", firstName: "Vivek", lastName: "Yadav").encodeAsJSON()
I am also able to render pogo in custom format. but I need it to do as rest full controller is doing for doamin.
Is there any way to do same thing with simple POGO defined in src/groovy directory?
Thanks in advance.