0

我有以下代码

    AddTicketCommand addTicketCommand = new AddTicketCommand(request.JSON)
    JsonBuilder jsonBuilder = new JsonBuilder()
    jsonBuilder {
        ticket {
            subject addTicketCommand.subject
            requester {
                name currentUser?.name
                email currentUser?.emailAddress
            }
            comment {
                body addTicketCommand.comment
            }
            custom_fields {
                [
                        {
                            id 21857727
                            value addTicketCommand.zenRequestType
                        },
                        {
                            id 21854146
                            value addTicketCommand.zenProductId
                        }
                ]
            }
        }
    }

addTicketCommand 对象在第 2 行不为空,但在 JsonBuilder 闭包中未定义。不能从闭包中访问 groovy 中的局部变量吗?

4

1 回答 1

1

您应该能够访问addTicketCommand封闭内部,如下所示。标记使用“括号”而不是“花括号”。

AddTicketCommand addTicketCommand = new AddTicketCommand(request.JSON)
JsonBuilder jsonBuilder = new JsonBuilder()
    jsonBuilder {
        ticket {
            subject addTicketCommand.subject
            requester {
                name currentUser?.name
                email currentUser?.emailAddress
            }
            comment {
                body addTicketCommand.comment
            }
            custom_fields ([ //Note the use of parenthesis
                {
                    id 21857727
                    value addTicketCommand.zenRequestType
                },
                {
                    id 21854146
                    value addTicketCommand.zenProductId
                }
            ]) ////Note the use of parenthesis
        }
    }
于 2013-07-09T01:57:43.737 回答