Groovy 将字符串解析为GString时出错

Groovy 将字符串解析为GString时出错,groovy,twilio-twiml,Groovy,Twilio Twiml,我在控制器类中使用groovy编写了这个TwiML """<?xml version="1.0" encoding="UTF-8"?> <Response> <Say voice="alice">Dear customer! This is an automated call from X_Y_Z.com to intimate you that the fare for your recently booked trip from

我在控制器类中使用groovy编写了这个TwiML

"""<?xml version="1.0" encoding="UTF-8"?>
  <Response>
    <Say voice="alice">Dear customer! This  is  an automated call from X_Y_Z.com to intimate  you that the  fare for  your recently  booked  trip from  ${from},to ${to} has increased by,${fare_Difference}\$ and now the total fare is,${totalFare}\$.</Say>
    <Pause length="1"/>
    <Gather num Digits="1" action="/notify/phone/selection/${phone_Number}/${from}/${to}/${fare_Difference}/${total_Fare}" method="POST" timeout="20" >
        <Say voice="Alice" >To accept the increased fare press 1. To talk to our travel agent press 2. To repeat press 3.</Say><Pause length="3"/>
    </Gather>
  </Response>"""
“”“
亲爱的客户!这是一个来自X_Y_Z.com的自动电话,通知您最近预订的从${from}到${to}的旅程票价增加了,${fare\u Difference}\$,现在总票价是,${totalFare}\$。
要接受加价,请按1。要与我们的旅行社交谈,请按2。要重复按3。
"""
现在,我的团队负责人要求我将TwiML移到数据库中

这里的问题是当我读它的时候 从数据库返回java.lang.String(我们在DAO层中使用hibernate),这是 由于未评估嵌入值而导致问题

e、 g而不是替换
“${from}”的值
作为
“纽约”
它在上面什么也不做,而把它作为
${from}

我怎样才能做到这一点

我试图将其转换为GString,但未能实现

任何帮助都将不胜感激。

这是一个已知的问题,尚未解决。解决方法是使用GroovyShell计算字符串表达式或使用GStringTemplateEngine。使用两种方法的示例代码:

String str = '''
<?xml version="1.0" encoding="UTF-8"?>
  <Response>
    <Say voice="alice">Dear customer! This  is  an automated call from X_Y_Z.com to intimate  you that the  fare for  your recently  booked  trip from  ${from},to ${to} has increased by,${fare_Difference}\\$ and now the total fare is,${totalFare}\\$.</Say>
    <Pause length="1"/>
    <Gather num Digits="1" action="/notify/phone/selection/${phone_Number}/${from}/${to}/${fare_Difference}/${total_Fare}" method="POST" timeout="20" >
        <Say voice="Alice" >To accept the increased fare press 1. To talk to our travel agent press 2. To repeat press 3.</Say><Pause length="3"/>
    </Gather>
  </Response>
'''


Map bindings = [from: 'Delhi', to: 'Mumbai', fare_Difference: '789', totalFare: '4567', phone_Number: '9876543210', total_Fare: '4567']

println (new GroovyShell(new Binding(bindings)).evaluate ('"""' + str + '"""'))

println new groovy.text.GStringTemplateEngine().createTemplate(str).make(bindings).writeTo(new StringWriter()).toString()
这是一个已知的错误,尚未修复。解决方法是使用GroovyShell计算字符串表达式或使用GStringTemplateEngine。使用两种方法的示例代码:

String str = '''
<?xml version="1.0" encoding="UTF-8"?>
  <Response>
    <Say voice="alice">Dear customer! This  is  an automated call from X_Y_Z.com to intimate  you that the  fare for  your recently  booked  trip from  ${from},to ${to} has increased by,${fare_Difference}\\$ and now the total fare is,${totalFare}\\$.</Say>
    <Pause length="1"/>
    <Gather num Digits="1" action="/notify/phone/selection/${phone_Number}/${from}/${to}/${fare_Difference}/${total_Fare}" method="POST" timeout="20" >
        <Say voice="Alice" >To accept the increased fare press 1. To talk to our travel agent press 2. To repeat press 3.</Say><Pause length="3"/>
    </Gather>
  </Response>
'''


Map bindings = [from: 'Delhi', to: 'Mumbai', fare_Difference: '789', totalFare: '4567', phone_Number: '9876543210', total_Fare: '4567']

println (new GroovyShell(new Binding(bindings)).evaluate ('"""' + str + '"""'))

println new groovy.text.GStringTemplateEngine().createTemplate(str).make(bindings).writeTo(new StringWriter()).toString()