zoukankan      html  css  js  c++  java
  • RestTemplate 使用示例

                RestTemplate client = new RestTemplate();
                    HttpHeaders headers = new HttpHeaders();
                //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
                headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    //            Map<String, Object> map = new HashMap<String, Object>();
    //            map.put("infohash", "1111111");
    //            map.put("name", "肖申克的救赎12");
    //            map.put("id", 111111);
    //            map.put("size", 111111);
                //HttpEntity<Map<String, Object>> request = new HttpEntity<Map<String, Object>>(map, headers);
                MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();
                params.add("infohash",infohash.getInfoHash());
                params.add("name", infohash.getName());
                params.add("id", infohash.getId());
                params.add("size", infohash.getLength());
                ResponseEntity<String> entity = client.postForEntity("http://localhost:9527/magnet/infohash/setdata", params, String.class);
                //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串;
                String body = entity.getBody();

    2


        

     2

            String body = null;
            RestTemplate client = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
            MovihInfohashDto infohash = movihInfohashService.create(resources);
            if(infohash.getId() != null) {
                   MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();
                   params.add("infohash",infohash.getInfoHash());
                   params.add("name", infohash.getName());
                   params.add("id", infohash.getId().toString());
                   params.add("size", infohash.getLength().toString());
                   headers.add("Content-Type", "application/x-www-form-urlencoded");
                   ResponseEntity<String> entity = client.postForEntity(JbibUtils.SETDATA, params, String.class);
                   //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串;
               body = entity.getBody();
               
            }
            JSONObject jo = JSONObject.parseObject(body);
            if(jo != null) {
                if(jo.getInteger("code") == 1) {
                    if(jo.getString("msg").equals("CREATED")) {
                        return new ResponseEntity<>(HttpStatus.OK);
                    }
                }
            }
            return new ResponseEntity<>(HttpStatus.NOT_EXTENDED);
        
  • 相关阅读:
    iOS 设置app语言中文,比如 copy中文,拍照按钮cancel 中文
    kCGImagePropertyExifDictionary 引用错误
    Objective-C中3种枚举比较及KVC两个小技巧
    xcode 调试程序 lldb 使用
    iOS kvo 结合 FBKVOController 的使用
    ios 推送app badge 数字累加操作
    推送未找到应用程序的“aps-environment”的权利字符串错误
    AVAudioPlayer播放在线音频文件
    MPMoviePlayerViewController 视频播放黑屏
    PHP 与 Redis 入门教程
  • 原文地址:https://www.cnblogs.com/roak/p/15079442.html
Copyright © 2011-2022 走看看