2016-02-04 165 views
4

我刚刚将我的应用程序从grails 3.0.13升级到了grails 3.1.1。在这样做的时候,我得到了一个有趣的JSON渲染问题。Grails 3.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 

而且它发生在约会.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渲染使用关系?

回答

2

我找到了答案,以我自己的问题在这里,我想我应该张贴的结果,万一别人有同样的问题。

该问题是因为我的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可能是因为开关没有工作到格姆5?无论如何,当我改为:

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

一切工作正常!我认为我不能使用customerId和resourceId属性(它们存储在约会本身上)有点奇怪。我认为这个问题来自访问其他域对象模型的其他行,但事实并非如此。