我使用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"
}
}
当我在控制器中创建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都不包含字段fromDate
和toDate
。可能是什么原因 ? (我不需要保持这个实例,我只需要将它呈现为JSON)。
编辑:
呈现的JSON看起来像:
{
"class": "com.scheduling.json.week.graphicalMode.ShiftUpdateResponse",
"id": 37,
"employees": [],
"salesPersonHours": null,
"success": 1
}
控制器代码:
@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
}
答案 0 :(得分:0)
抱歉,noob错误。问题可能是因为,在不同的包中存在同一文件的另一个副本。删除重复的固定内容。