json - Grails JSON渲染

标签 json grails groovy gorm

我使用Grails 2.3.8。我注意到Grails JSON呈现中的奇怪行为。我有一个映射到mongodb的域类ShiftUpdateResponse,如下所示。

     class ShiftUpdateResponse {
        List<Employee> employees
        SalesPersonHours salesPersonHours;
        Integer fromDate
        Integer toDate
        Integer success

        static mapWith = "mongo"
        static hasMany = [employees:Employee]
        static embedded = ['employees','salesPersonHours']
        static constraints = {
           salesPersonHours nullable:true
           success nullable:true 
           fromDate nullable:true 
           toDate nullable:true 
        }

        String toString() {
           " $employees"
        }
      }

当我在 Controller 中创建ShiftUpdateResponse对象并使用以下代码段将其呈现为JSON时:
   def  sur = new ShiftUpdateResponse(employees:[], salesPersonHours:[], success:1, fromDate:from, toDate:to)
   println "from " + from
   println "to " + to
   println "from sur " + sur.fromDate
   println "to sur " + sur.toDate
   JSON.use('deep')
   render sur as JSON

我注意到以下几点
  • 当我在渲染前不保存创建的实例sur时,控制台输出为
    from 20130624
    to 20130623
    from sur null
    to sur null
    
  • 当我在渲染之前保存创建的实例时,控制台输出为
    from 20130624
    to 20130623
    from sur null
    to sur 20130623
    

  • 无论哪种情况,呈现的JSON都不包含fromDatetoDate字段。可能是什么原因 ? (我不需要保留该实例,只需要将其呈现为JSON)。

    编辑:

    呈现的JSON如下所示:
        {
    "class": "com.scheduling.json.week.graphicalMode.ShiftUpdateResponse",
    "id": 37,
    "employees": [],
    "salesPersonHours": null,
    "success": 1
        }
    

    Controller 代码:
        @Transactional
        def updateShift(){
    
        def reqJson = request.JSON
        somService.updateShift(reqJson)
    
    
        def from
        def to
        def week = WeekJson.get(reqJson.week_id)
    
        // code to construct from and to from week
    
        def sur = new ShiftUpdateResponse(employees:[],salesPersonHours:[],success:1,fromDate:from,toDate:to)
    
        //sur.save(flush:true)
    
        println "from "+from
        println "to "+to
        println "from sur "+sur.fromDate
        println "to sur "+sur.toDate
    
    
        JSON.use('deep')
        render sur as JSON
    
    }
    

    最佳答案

    对不起,菜鸟错误。问题可能是因为在不同的程序包中存在相同文件的另一个副本。删除重复的固定东西。

    关于json - Grails JSON渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24574375/

    相关文章:

    javascript - 如何在从服务器获取数据的动态表的每一行中添加下拉列表?

    javascript - Vue,比较两个数组并检查结果是否匹配

    grails - 对域对象字段的更改是否可以将grails域视为已修改/变脏?

    Grails - save() 在集成测试中因 MissingMethodExcepition 而失败

    android - 如何从 Gradle Experimental Plugin 中的任务访问 versionCode

    java - hasNext 跳过 HashMap 中的第一个值

    java - 如何在 Spring Boot 中反序列化/序列化类型 Geometry?

    jquery - Ajax调用无法正常工作

    json - 将对象和值呈现为JSON作为Ajax响应

    regex - 正则表达式从Groovy GString中删除\“?