Grails 3.1 json渲染

时间:2016-02-04 18:17:45

标签: json grails hibernate-session grails-3.1

我刚刚将我的应用程序从grails 3.0.13升级到grails 3.1.1。这样做我得到了一个有趣的JSON渲染问题。

我一直在使用自定义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
    ]
}

当我尝试在grails 3.1.1中使用它时,我收到以下错误:

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

它出现在appointment.resource?.name行上。我已经尝试了不同的方法来解决这个问题并成功解决了一个用例,但现在我在另一个地方遇到了同样的错误。要改变这种情况似乎是错误的做法。

在我的域类中,我已经声明了这样的关系:

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

所以我认为我应该热切地加载资源和客户领域? (我之前在grails 3.0.14用户"客户懒惰:假"它工作。

然而现在它还没有奏效。那么会话如何在gorm 5中发挥作用?为了能够在json渲染中继续使用关系,我该怎么做?

1 个答案:

答案 0 :(得分:2)

我在这里找到了自己问题的答案,并认为我应该发布结果,以防其他人遇到同样的问题。

这个问题是因为我的JSON marshaller。我使用了以下内容:

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属性(存储在约会本身上)有点奇怪。我认为问题来自访问其他域对象上的模型的其他行,但事实并非如此。