Grails 4:自定义域模型编组器不适用于 Grails 4
Grails 4: Custom Domain Model Marshaller Not Working On Grails 4
我现在的问题是我从 grails 2.4.5 到 grails 创建的自定义对象编组器3.3.0 不适用于 grails 4.0.0。 Grails 4 默认响应域模型,而不是我创建的自定义模型。
下面是我的代码。请检查,如果您发现有问题,请告诉我,如果您能帮助我,我将很高兴。
ResponseSender.groovy
package com.problem.solve.common
import org.springframework.http.HttpStatus
trait ResponseSender {
void sendResponse() {
render status: HttpStatus.NO_CONTENT
}
void sendResponse(def responseData) {
respond (responseData)
}
void sendResponse(HttpStatus status, def responseData) {
response.status = status.value()
respond (responseData)
}
}
这个ResponseSender.groovy特性是在控制器上实现的。
MarshallerInitializer.groovy
package com.problem.solve.marshaller
class MarshallerInitializer {
CustomObjectMarshallers customObjectMarshallers
void initialize() {
customObjectMarshallers.register()
}
}
这个MarshallerInitializer.groovy会在bootstrap初始化的时候调用。
package com.problem.solve.marshaller
class CustomObjectMarshallers {
List marshallers = []
void register() {
marshallers.each {
it.register()
}
}
}
这CustomObjectMarshallers.groovy将注册所有编组器。
UserMarshaller.groovy
package com.problem.solve.marshaller.marshalls
import com.problem.solve.security.User
import grails.converters.JSON
class UserMarshaller {
void register() {
JSON.registerObjectMarshaller(User) { User user ->
return [
id: user.id,
fullName: user.fullName,
username: user.username,
emailAddress: user.emailAddress,
roles: user.authorities.authority,
dateCreated: user.dateCreated,
lastUpdated: user.lastUpdated,
_entityType: 'User'
]
}
}
这个UserMarshaller.groovy是我想从领域模型转换成json的示例领域模型响应。
resources.groovy
import com.problem.solve.marshaller.CustomObjectMarshallers
import com.problem.solve.marshaller.MarshallerInitializer
import com.problem.solve.marshaller.marshalls.*
// Place your Spring DSL code here
beans = {
customObjectMarshallers(CustomObjectMarshallers) {
marshallers = [
new UserMarshaller()
]
}
marshallerInitializer(MarshallerInitializer) {
customObjectMarshallers = ref('customObjectMarshallers')
}
}
此设置的问题不适用于 grails 4,但此设置适用于 grails 2.4.5 和 grails 3.3.0。
我真的需要你们的帮助。
非常感谢:)
我通过创建 DomainModelResponseDto 作为响应域模型解决了这个编组器问题。
示例:
class UserResponseDto {
String id
String username
String email
UserResponseDto(User user) {
id = user.id
username = user.username
email = user.email
}
}
我有另一个解决这个问题的方法。编组器正在处理问题是视图。我必须删除在创建域时生成的视图 class 并且所有内容都与当前编组器和设置完美配合。
我现在的问题是我从 grails 2.4.5 到 grails 创建的自定义对象编组器3.3.0 不适用于 grails 4.0.0。 Grails 4 默认响应域模型,而不是我创建的自定义模型。
下面是我的代码。请检查,如果您发现有问题,请告诉我,如果您能帮助我,我将很高兴。
ResponseSender.groovy
package com.problem.solve.common
import org.springframework.http.HttpStatus
trait ResponseSender {
void sendResponse() {
render status: HttpStatus.NO_CONTENT
}
void sendResponse(def responseData) {
respond (responseData)
}
void sendResponse(HttpStatus status, def responseData) {
response.status = status.value()
respond (responseData)
}
}
这个ResponseSender.groovy特性是在控制器上实现的。
MarshallerInitializer.groovy
package com.problem.solve.marshaller
class MarshallerInitializer {
CustomObjectMarshallers customObjectMarshallers
void initialize() {
customObjectMarshallers.register()
}
}
这个MarshallerInitializer.groovy会在bootstrap初始化的时候调用。
package com.problem.solve.marshaller
class CustomObjectMarshallers {
List marshallers = []
void register() {
marshallers.each {
it.register()
}
}
}
这CustomObjectMarshallers.groovy将注册所有编组器。
UserMarshaller.groovy
package com.problem.solve.marshaller.marshalls
import com.problem.solve.security.User
import grails.converters.JSON
class UserMarshaller {
void register() {
JSON.registerObjectMarshaller(User) { User user ->
return [
id: user.id,
fullName: user.fullName,
username: user.username,
emailAddress: user.emailAddress,
roles: user.authorities.authority,
dateCreated: user.dateCreated,
lastUpdated: user.lastUpdated,
_entityType: 'User'
]
}
}
这个UserMarshaller.groovy是我想从领域模型转换成json的示例领域模型响应。
resources.groovy
import com.problem.solve.marshaller.CustomObjectMarshallers
import com.problem.solve.marshaller.MarshallerInitializer
import com.problem.solve.marshaller.marshalls.*
// Place your Spring DSL code here
beans = {
customObjectMarshallers(CustomObjectMarshallers) {
marshallers = [
new UserMarshaller()
]
}
marshallerInitializer(MarshallerInitializer) {
customObjectMarshallers = ref('customObjectMarshallers')
}
}
此设置的问题不适用于 grails 4,但此设置适用于 grails 2.4.5 和 grails 3.3.0。
我真的需要你们的帮助。
非常感谢:)
我通过创建 DomainModelResponseDto 作为响应域模型解决了这个编组器问题。
示例:
class UserResponseDto {
String id
String username
String email
UserResponseDto(User user) {
id = user.id
username = user.username
email = user.email
}
}
我有另一个解决这个问题的方法。编组器正在处理问题是视图。我必须删除在创建域时生成的视图 class 并且所有内容都与当前编组器和设置完美配合。