grails写接口

来源:互联网 发布:专业做网站的公司知乎 编辑:程序博客网 时间:2024/06/15 19:28

一、传递json数据:

import groovy.json.JsonOutputimport grails.converters.JSON

接收:
获取前台传过来的json。 使用

def jsonObject = JSON.parse(params.jsonObject)

如果是接口的话,接受json数据,使用

     def jsonObject = request.JSON

返回:
方式一:

 def errors = [status: 400, message:"缺少必要的参数!"]     render JsonOutput.toJson(errors)

方式二:

    render ([status:400 , message:"缺少必要的参数!"]) as JSON
def clientip = request.getRemoteAddr()
def a = null as JSONprintln a  //nulldef b = "" as JSON  //报错
0 0
原创粉丝点击