我正在使用 grails 2.5.0。我想将src/groovy
class 渲染为JSON
和 custom JSON
。如果我对域类执行相同的操作,那么它对我来说工作正常。我想要与 src/groovy 类相同的东西。对于使用 json 和自定义 json 呈现数据,我遵循了这个链接。
我试过这些东西。在 src/groovy 中创建 pogo 类。
class UserDTO {
String username
String lastName
String firstName
String email
}
创建了一个具有宁静功能的控制器。
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")
}
}
在 urlMapping.groovy 上做了一个条目。
"/vivek"(resources:'renderDTO')
在 resources.groovy 中为自定义渲染器注册了新的 mime 类型。
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')]
}
在 mime 类型中添加了新的 mime 类型。
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']
]
在 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
}
}
当我跑步时,我遇到了异常。
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'
如果我像这样更改控制器。
respond new UserDTO(username: "vivek", firstName: "Vivek", lastName: "Yadav").encodeAsJSON()
我能够将其转换为 JSON 格式。此外,如果用户使用此语法。
JSON.use("pogodetails")
respond new UserDTO(username: "vivek", firstName: "Vivek", lastName: "Yadav").encodeAsJSON()
我还能够以自定义格式呈现 pogo。但我需要它来完成其余的完整控制器正在为域做的事情。
有没有办法用 src/groovy 目录中定义的简单 POGO 做同样的事情?
提前致谢。