RestTemplate 使用示例

RestTemplate 使用示例| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |

| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 15079442| RestTemplate 使用示例| 2021-07-30T14:27:00| | BlogPost|

复制代码

            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 </span>=<span style="color: #000000;"> JSONObject.parseObject(body);
    </span><span style="color: #0000ff;">if</span>(jo != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">if</span>(jo.getInteger("code") == 1<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span>(jo.getString("msg").equals("CREATED"<span style="color: #000000;">)) {
                </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ResponseEntity&lt;&gt;<span style="color: #000000;">(HttpStatus.OK);
            }
        }
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> ResponseEntity&lt;&gt;<span style="color: #000000;">(HttpStatus.NOT_EXTENDED);
</span></pre>
复制代码

 

| 648658| | 2021-08-05T16:33:00| false| | 2021-07-30T14:27:02.77| true| RestTemplate client = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); //定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON headers.setContentT| Anonymous|
posted @   RalphLauren  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示