grailsrestxml呈现

grailsrestxml呈现,xml,parsing,rest,grails,groovy,Xml,Parsing,Rest,Grails,Groovy,假设我的控制器中有这样的场景: def nr_1 = params.first_nr def nr_2 = params.second_nr def result def erro = 'no' if(nr_1.isInteger() && nr_2.isInteger()) result = nr_1.toInteger() * nr_2.toInteger() else erro = 'yes' if(erro.equals('yes')) [sm

假设我的控制器中有这样的场景:

def nr_1 = params.first_nr
def nr_2 = params.second_nr
def result
def erro = 'no'

if(nr_1.isInteger() && nr_2.isInteger())
    result = nr_1.toInteger() * nr_2.toInteger()
else
    erro = 'yes'

if(erro.equals('yes'))
    [sms : 'Please introduce only 2 numbers!']
else
    [sms: 'The result of the multiplication of ' + nr_1 + ' with ' + nr_2 + ' is ' + result]
这将返回到我的gsp视图,并已成功完成。现在我想将其转换为REST访问Web服务。在我看来,我必须手动创建如下标记:

<firstNumber>nr_1</firstNumber>
<secondNumber>nr_1</secondNumber>   
<result>result</result>  
nr\u 1
nr_1
后果

然后返回rest请求。我如何才能做到这一点(通过提供HTML和XML响应,对于XML,只解析最后的XML标记)。

控制器中的格式是否对您有用

您可以创建一个表示请求的对象,并将请求的内容放入其中

class Multiplication
{
  String nr_1
  String nr_2
  String result
}
它将使您能够使用
呈现为XML
在操作中生成XML:

def multiplication = new Multiplication(nr_1: params.first_nr,
                                        nr_2: params.second_nr)
def error = 'no'
  if (multiplication.nr_1.isInteger() && multiplication.nr_2.isInteger())
    multiplication['result'] = multiplication.nr_1.toInteger() * multiplication.nr_2.toInteger()
  else
    error = 'yes'

if (error == 'yes')
{
  [sms: 'Please introduce only 2 numbers!']
}
withFormat {
  html sms: "The result of the multiplication of $multiplication.nr_1 with $multiplication.nr_2 is $multiplication.result"
  xml { render multiplication as XML }
}
不要忘记在控制器的开头导入grails.converters.