使用第三方文档json请求基本操作

curl请求测试

  • 拿文档里的演示数据,先请求测测看有没通
  • -i:表示包含响应头信息。
    -X POST:指定使用 POST 方法发送请求。
    -H 'Content-Type:application/json':设置请求头中的 Content-Type 为 application/json,表示请求体的格式是 JSON 格式。
    -H 'token:xxxxx':设置请求头中的 token 字段为指定的值
    -d "{\"msgId\":\"xxxxx\"}":指定请求体内容为 JSON 格式的数据,其中包含一个名为 msgId 的字段,其值为 “xxxxx”。
    http://tracxxxx.xx.cn/api/v2/receipt/xxx:指定发送请求的目标 URL。
  • -d 中的最外层如果是[]就是代表数组,如果是{}则代表一个对象
curl -i -X POST -H 'Content-Type:application/json' -H 'token:xxxxx' -d "{\"msgId\":\"xxxxx\"}" http://tracxxxx.xx.cn/api/v2/receipt/xxx

hutool方式

准备

  • 导入依赖alibaba(注意hutool也有json转化的api,但此演示的JSONObject 则使用阿里巴巴的依赖)
  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>

演示

  • 通过第三方传过来的ticket,调用接口文档中的接口httpRequesturl,获取对应返回参数
  • 请求若要指定请求头参数 则新建一个 Map<String, String> headers = new HashMap<>(); 存放各请求头参数, 放入以下代码http请求httpRequest.contentType("application/json").addHeaders(headers).xxxxxxxx().xxxx() 完成新增

    private static final String userEndpoint = "http://xxxxxx:端口";

 private HttpRequest initUserReq(String api){
        String token = getAccessToken(userEndpoint);
        if (Objects.isNull(token)){
            throw new BizException("初始化请求失败");
        }
       return HttpUtil.createPost(userEndpoint + api+"?"
                +"accessToken="+token);
    }
   private static final String VALIDA_TICKET="/xxx/xxx";

 public EduYunUserInfo validaTicket(String ticket){
        HttpRequest httpRequest = initUserReq(VALIDA_TICKET);
        //整合请求接口所需参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("ticket",ticket);
        String request = JSONObject.toJSONString(map);
        //请求第三方接口
        String body = httpRequest.contentType("application/json").body(request).execute().body();
        log.info("返回的应答报文{}",body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String code = jsonObject.getString("retCode");
        if (!code.equals(SUCCESS)){
            log.info("验证ticket失败,code:{}",code);
            return null;
        }
        //将返回数据强转成我们自定义实体类(属性名要对应)
        JSONObject data = jsonObject.getJSONObject("data");
        EduYunUserInfo eduYunUserInfo = data.toJavaObject(EduYunUserInfo.class);
        return eduYunUserInfo;
    }

RestTemplate方式

    private static final String userEndpoint = "http://xxxxxx:端口号";
    private static final String LOGIN_API=userEndpoint+"/xxxx/xxxxx";


  public R<UserLoginNewResp> getCurrentUser(UserLoginReq req) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(LOGIN_API, req, String.class);
        String body = stringResponseEntity.getBody();
        log.info("返回的应答报文{}",body);
        if (StringUtils.isBlank(body)){
            return R.failed("找不到用户数据");
        }
        JSONObject jsonObject = JSONObject.parseObject(body);
        Integer code = (Integer) jsonObject.get("code");
        if (code!=0){
            return R.failed((String) jsonObject.get("msg"));
        }
        //将返回数据强转成我们自定义实体类(属性名要对应)
        JSONObject entity = jsonObject.getJSONObject("entity");
        UserLoginNewResp userLoginNewResp = entity.toJavaObject(UserLoginNewResp.class);
        return R.ok(userLoginNewResp);
    }

json对象字符串 转 map

  • import com.fasterxml.jackson.databind.ObjectMapper;
  • 模拟数据
    在这里插入图片描述
@Test
    public void test() throws IOException {
        ObjectMapper MAPPER = new ObjectMapper();
        String testA = "{\"result\":{\"Status\":1,\"Isp\":\"中国移动\"},\"code\":\"0\"}";
        Map map = null;
        map = MAPPER.readValue(testA, Map.class);
        System.out.println(map.get("code").toString());
        Integer a = (Integer) ((Map) map.get("result")).get("Status");
        System.out.println(a);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值