使用 RestTemplate 发送 post请求(json参数)

时间:2025-02-22 07:00:30

使用json作为参数版本

import ;
import .*;
import ;
import ;

//调用post请求
    public static JSONObject callPostAPI(String url, JSONObject header, JSONObject body) {

            RestTemplate client = new RestTemplate();

            HttpHeaders headers = new HttpHeaders();
            (MediaType.APPLICATION_JSON);

            //设置其他头信息
            Iterator iterator = ();
            while(()){
                String key = (String) ();
                String value = (key);
                (key, value);
            }

            HttpEntity<String> formEntity = new HttpEntity<String>((), headers);
            JSONObject json = (url, formEntity, ).getBody();
            ("json:"+());
            return json;
    }