grails发送POST请求,接受请求

grails发送POST请求,接受请求
//此连接地址为配置到app-config.properties下面

def http = new HTTPBuilder(ConfigurationHolder.config.entrust.serverUrl)

//----发送请求 

/**
   *发送请求 ,参数根据具体接口内容拟定

   * @param orders 订单号集合,必须参数
   * @param auditor 审核人  必须参数
   * @return {result: 'true or false', errorMsg: '错误信息'}
   * result: true为成功, false 为失败,
   * errorMsg: 当result为false时,返回误原因,
   * @throws Exception
   */
  def refuseCheckForEntrust(orders,auditor) throws Exception {
    http.request(POST, JSON) { req ->
      uri.path = 'yourControllerName/yourMethodName'
      body = [orders:orders,auditor:auditor] //传递的参数,将要按照你所指定的格式进行发送
       req.getParams().setParameter("http.connection.timeout", new Integer(60000));
req.getParams().setParameter("http.socket.timeout", new Integer(60000));
      response.success = { resp, json ->
        return json
      }
      response.failure = { resp ->
        throw new Exception('发送POST请求失败"+resp)
      }
    }
  }

//---接受POST请求端解析,(以grails的controller为例,JAVA端的Action类似)

//1、第一种方式按照JAVA中的JSON-LIB方式转换为对象

           String jsonStr = request.JSON;//此处可以弱引用声明
            log.info 'process charge .'
            Charge charge = new Charge()
            charge = (Charge) JSONObject.toBean(JSONObject.fromObject(jsonStr), Charge.class);

//第二种方式按照grails弱引用的特点去取单独的某个参数

def jsonReq = request.JSON;//此处可以弱引用声明
def name = jsonReq.name

String jsonStr = jsonReq as String 

//返回请求端内容

def result = [result: false, errorMsg: "该账号已经存在,请重新输入"]
 render result as JSON


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值