演示代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.http.HttpRequest;
/**
* cf
*/
public class TqOdpServiceClient {
private static String url="url";;
public static String execute(String http,String params) {
JSONObject response = JSONObject.parseObject(
HttpRequest.post(http + url)
.body(params)
.execute()
.body()
);
return response;
}
}
演示代码
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.http.HttpRequest;
/**
* cf
*/
public class TqOdpServiceClient {
private static String url="url";;
public static String execute(String http) {
HashMap<String, Object> params = new HashMap<>(2);
params.put("test1", "测试数据");
params.put("test2", "测试数据");
JSONObject response = JSONObject.parseObject(
HttpRequest.post(http + url)
.form(params)
.execute()
.body()
);
return response;
}
}
演示代码:可以看到 String newParams = JSON.toJSONString(params);
将 HashMap 类型的数据转换为字符串类型,就可以作为字符串被传递到 body 内,后面就是对应接口的后端数据处理问题了。
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import cn.hutool.http.HttpRequest;
/**
* cf
*/
public class TqOdpServiceClient {
private static String url="url";;
public static String execute(String http) {
HashMap<String, Object> params = new HashMap<>(2);
params.put("test1", "测试数据");
params.put("test2", "测试数据");
String newParams = JSON.toJSONString(params);
JSONObject response = JSONObject.parseObject(
HttpRequest.post(http + url)
.body(newParams)
.execute()
.body()
);
return response;
}
}
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
/**
* cf
*/
public class TqOdpServiceClient {
private static String url="url";;
public static String execute(String http, String accessToken) {
JSONObject response = JSONObject.parseObject(
HttpRequest.get(http + url)
.header(Header.AUTHORIZATION, "Bearer ".concat(accessToken))
.execute()
.body()
);
return response;
}
}