一个最简单的 http 调用写法

http 调用方法 特别多老式的方式 代码量特别多,方式繁琐且不易且限制很多,下面这个方法可以节省很多代码:

例子 一、

public static void main(String[] args) {
		VipSmsCallBackReq req = new VipSmsCallBackReq();
		req.setBatchNo("20181023163320");
		req.setMobile("13928807044");
		req.setSmsStutas("2");
		RestTemplate rest = new RestTemplate();
		RequestEntity<VipSmsCallBackReq> request = RequestEntity.post(URI.create("http://172.19.3.111:8081/sms/call-back")).header("Content-Type", "application/json").body(req);
		BaseResponse stu2 = (BaseResponse) JSON.parseObject(rest.exchange(request, String.class).getBody(),BaseResponse.class);
		System.out.println(rest.exchange(request, String.class).getBody());   //打印的原始请求,未做任何转换 不可以和上一行同时存在,否则会执行2次
		 
}

 

有时候 无需建对象可以直接使用JSONObject 类型

 

例子 二、

如果是在地址栏拼接的参数那请求时就不需要body 而是直接 build ,对比一下:

例子 三、

 需要我们带上 header 参数的情况下:

postman 请求示例:

 

 代码如下:

public Result getCftoken(CashFreeTokenParamDto param) {
        try{
            log.info("开始获取cftoken订单ID={}",param.getOrderId());
            RestTemplate rest = new RestTemplate();
            RequestEntity request = RequestEntity.post(URI.create("https://api.cashfree.com/api/v2/cftoken/order"))
                    .header("Content-Type", "application/json")
                    .header("x-client-id", "4353434553456456432343")
                    .header("x-client-secret", "756453243rewfdsfvdfewr345334423r2ds")
                    .body(param);
            String str = rest.exchange(request, String.class).getBody();
            
            
            ResultObj result = JSON.parseObject(str, new TypeReference<ResultObj>(){});
            JSONObject obj = new JSONObject();
            obj.put("token",result.getCftoken());
            obj.put("appId",appId);
            obj.put("stage",stage);
            return Result.ok(obj);
        }catch (Exception e){
            return Result.fail("Get token failure");
        }
    }

代码截图: 

 

例子 四、Body 纯表单提交方式:

postman 截图:

public static void main(String[] args) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
        map.add("appId", "1431326ee5695435646346342762311");
        map.add("secretKey", "d762dd7565c5d4235435434454dzzdgfhsd4ec9ea");
        map.add("orderId", "3214234883332");
        map.add("orderAmount", "1");
        map.add("orderCurrency", "INR");
        map.add("orderNote", "test order");
        map.add("customerEmail", "rohharma@qq.com");
        map.add("customerName", "RodshitSdsdsha222222rma");
        map.add("customerPhone", "9909612223485");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.postForEntity("https://test.cashfree.com/open/api/v1/order/create", request , String.class );
        System.out.println(JSON.toJSON(response));
    }

代码截图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值