Java语法:restTemplate发送http请求

public static Boolean checkRole(String userId) {
    RestTemplate restTemplate = new RestTemplate();
    String url="http://localhost:8181/userRole/checkRole/"+userId;
    Result forObject = restTemplate.getForObject(url, Result.class);
    Boolean data = (Boolean) forObject.getData();
    return data;
}

 String url = "http://127.0.0.1:5000/storeMilvus";
        JSONObject requestMap = new JSONObject();
        requestMap.put("id", id);
        requestMap.put("filepath",filePath);
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, requestMap, JSONObject.class);

package com.atguli.common.demo;
 
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
 
import java.util.ArrayList;
 
/**
 * User: ldj
 * Date: 2022/3/25
 * Time: 1:30
 * Description: restTemplate
 */
@Slf4j
public class RestTemplateDemo {
    public static void main(String[] args) throws JsonProcessingException {
 
        String url = "http://locahost:8080/test";
        
        RestTemplate restTemplate = new RestTemplate();
 
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        
        ArrayList<Object> similarList = new ArrayList<>();
        similarList.add("1");
        similarList.add("2");
        similarList.add("3");
 
        JSONObject requestMap = new JSONObject();
        requestMap.put("name", "ldj");
        requestMap.put("age", "15");
        requestMap.put("similarList",similarList);
 
        HttpEntity<JSONObject> entity = new HttpEntity<>(requestMap, headers);
 
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String similarJSON = objectMapper.writeValueAsString(requestMap);
            log.info("similarJSON:{}",similarJSON);
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        //使用JSONObject,不需要创建实体类VO来接受返参,缺点是别人不知道里面有哪些字段,即不知道有那些key
        JSONObject body1 = restTemplate.postForObject(url, entity, JSONObject.class);
        log.info("body1:{}",objectMapper.writeValueAsString(body1));
 
        ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(url, requestMap, JSONObject.class);
        JSONObject body2 = responseEntity.getBody(); //响应体
        HttpStatus statusCode = responseEntity.getStatusCode(); //状态码
        HttpHeaders headers1 = responseEntity.getHeaders();//获取到头信息
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟蹦迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值