2
votes

My current problem right now is the custom object marshaller that I created since grails 2.4.5 to grails 3.3.0 is not working on grails 4.0.0. Grails 4 respond the domain model by default not the custom made I create.

Below are the codes I have. Please review and if you find theres something wrong please do let me know guys, I will be glad if you can help me with this.


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)
    }
}

This ResponseSender.groovy trait is implemented on the controller.


MarshallerInitializer.groovy

package com.problem.solve.marshaller

class MarshallerInitializer {

    CustomObjectMarshallers customObjectMarshallers

    void initialize() {
        customObjectMarshallers.register()
    }
}

this MarshallerInitializer.groovy will be called when bootstrap initialize.

package com.problem.solve.marshaller

class CustomObjectMarshallers {

    List marshallers = []

    void register() {
        marshallers.each {
            it.register()
        }
    }
}

This CustomObjectMarshallers.groovy will going to register all marshallers.


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'
            ]
        }
    }

This UserMarshaller.groovy is a sample domain model that I want to convert from domain model to json response.


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')
    }
}

The problem with this setup is not working on grails 4, but this setup are working with grails 2.4.5 and grails 3.3.0.

I really need your help guys.

Thank you so much :)

2
I solve this marshaller problem by creating DomainModelResponseDto as respond domain model. Example: class UserResponseDto { String id String username String email UserResponseDto(User user) { id = user.id username = user.username email = user.email } } - redwolfgang20

2 Answers

0
votes

I solve this marshaller problem by creating DomainModelResponseDto as respond domain model.

Example:

class UserResponseDto {
    String id
    String username
    String email

    UserResponseDto(User user) {
        id = user.id
        username = user.username
        email = user.email
    }
}
0
votes

I have another solution to this problem. The marshaller is working the problem is the views. I have to delete the views that are generated when creating a domain class and all is working perfectly with the current marshaller and setup.