Grails 3.1 json rendering

I just updated my application from grails 3.0.13 to grails 3.1.1. In doing so, I had an interesting problem with JSON rendering.

I am using a custom JSON marshaler:

JSON.registerObjectMarshaller(Appointment) { Appointment appointment ->
    [
        id: appointment.id,
        version: appointment.version,
        resourceChangeable: appointment.resourceChangeable,
        start: appointment.startTime.format("yyyy-MM-dd'T'HH:mm:ss"),
        end: appointment.endTime.format("yyyy-MM-dd'T'HH:mm:ss"),
        displayStartTime: appointment.displayStartTime,
        displayEndTime: appointment.displayEndTime,
        title: appointment.description,
        customerId: appointment.customerId,
        customerName: appointment.customer?.fullName,
        customerPhone: appointment.customer?.cellPhone,
        resourceId: appointment.resourceId,
        resourceName: appointment.resource?.name,
        editUrl: appointment.editUrl,
        updateUrl: appointment.updateUrl,
        errors: appointment.errors,
        eventBackgroundColor: appointment.resource?.backgroundColor,
        notes: appointment.notes
    ]
}

When I try to use this in grails 3.1.1, I get the following error:

No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here. Stacktrace follows:
org.grails.web.converters.exceptions.ConverterException: java.lang.IllegalStateException: No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here
    at grails.converters.JSON.value(JSON.java:184) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.convertAnother(JSON.java:144) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.value(JSON.java:184) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.render(JSON.java:119) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.render(JSON.java:132) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.artefact.controller.support.ResponseRenderer$Trait$Helper.render(ResponseRenderer.groovy:191) ~[grails-plugin-controllers-3.1.1.jar:3.1.1]
    at se.easycalendar.admin.CalendarController.getAppointments(CalendarController.groovy:39) ~[main/:na]
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53) ~[spring-security-core-3.0.3.jar:na]
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62) ~[spring-security-core-3.0.3.jar:na]
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58) ~[spring-security-core-3.0.3.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_72]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_72]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_72]
Caused by: java.lang.IllegalStateException: No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here
    at BootStrap$_closure1$_closure3.doCall(BootStrap.groovy:30) ~[main/:na]
    ... 13 common frames omitted

And this happens in the destination string .resource? .Name. I tried different ways to get around this and succeed in a workaround for one user, but now I am getting the same error elsewhere. To change it everywhere seems like the wrong way to do it.

In my domain class, I declared the following relationship:

class Appointment {
    String uuid = UUID.randomUUID().toString()
    Date startTime
    Date endTime
    String description
    String notes
    Boolean resourceChangeable = true

    Date dateCreated
    Date lastUpdated

    static belongsTo = [resource: Resource, customer: Customer, serviceEntityResource: ServiceEntityResource, sms: Sms]

    static constraints = {
        customer nullable: true
        uuid maxSize: 40
        serviceEntityResource nullable: true
        description maxSize: 100
        notes maxSize: 500, nullable: true, blank: true
        sms nullable: true
    }

    static mapping = {
        startTime index: 'appointment_start_time_idx'
        sms cascade: 'none'
        sort "startTime"
        customer fetch: 'join'
        resource fetch: 'join'
     }
}

So, I thought I should look forward to loading resource and client fields? (I previously had a user "lazy client: false" in grails 3.0.14, and it worked.

. , gorm 5? , json?

+4
1

, , - .

- JSON. :

JSON.registerObjectMarshaller(Appointment) { Appointment appointment ->
    [
    id: appointment.id,
    version: appointment.version,
    resourceChangeable: appointment.resourceChangeable,
    start: appointment.startTime.format("yyyy-MM-dd'T'HH:mm:ss"),
    end: appointment.endTime.format("yyyy-MM-dd'T'HH:mm:ss"),
    displayStartTime: appointment.displayStartTime,
    displayEndTime: appointment.displayEndTime,
    title: appointment.description,
    customerId: appointment.customerId,
    customerName: appointment.customer?.fullName,
    customerPhone: appointment.customer?.cellPhone,
    resourceId: appointment.resourceId,
    resourceName: appointment.resource?.name,
    editUrl: appointment.editUrl,
    updateUrl: appointment.updateUrl,
    errors: appointment.errors,
    eventBackgroundColor: appointment.resource?.backgroundColor,
    notes: appointment.notes
    ]
}

:

customerId: appointment.customerId,
resourceId: appointment.resourceId,

grails 3.0.x, grails 3.1.x, , - gorm 5? , :

customerId: appointment.customer?.id,
resourceId: appointment.resource?.id,

! , customerId resourceId ( ). , , , .

+2

Source: https://habr.com/ru/post/1627336/


All Articles