java中调用RESTful的post接口

做项目时经常会遇到要与第三方联调接口的情况,对方提供一个接口文档,接下来就调用获取数据

/**
     * 发送POST请求,参数是Map<String,Object> 类型
     * @param url 请求API的URL
     * @param data POST数据,Map类型
     * @return API返回的JSON数据
     * @throws Exception
     */
    public static String sendPostRequest(String url, Map<String, Object> data,String token) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setRequestProperty("API-TOKEN", token);


        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());

        String postData = convertMapToJsonString(data);
        wr.writeBytes(postData);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }

    /**
     * 把Map类型数据转换成JSON字符串
     * @param map Map类型数据
     * @return 转换后的JSON字符串
     * @throws Exception
     */
    private static String convertMapToJsonString(Map<String, Object> map) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(map);
    }

url:接口地址

data:请求参数,是个Map,也可以是json字符串(json字符串就不需要 “String postData = convertMapToJsonString(data);”这行代码)

token: 对方接口的token校验,需要就传输,不需要就不用

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Java代码示例,用于调用Flink RESTful接口提交批处理作业: ```java import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClientBuilder; import java.io.IOException; public class FlinkRestClient { public static void main(String[] args) throws IOException { String flinkRestUrl = "http://localhost:8081/jars/upload"; // Flink RESTful接口URL String jarPath = "/path/to/your/jar/file"; // 批处理作业jar包路径 // 构造请求体 String requestBody = "{\"entryClass\":\"com.example.batch.BatchJob\"," + "\"programArgs\":[\"--input\",\"/path/to/input\",\"--output\",\"/path/to/output\"]}"; // 构造POST请求 HttpPost postRequest = new HttpPost(flinkRestUrl); postRequest.setHeader("Accept", "application/json"); postRequest.setHeader("Content-type", "application/json"); postRequest.setHeader("User-Agent", "FlinkRestClient/1.0"); // 设置jar文件实体 StringEntity fileEntity = new StringEntity(jarPath, ContentType.APPLICATION_OCTET_STREAM); postRequest.setEntity(fileEntity); // 设置请求体实体 StringEntity bodyEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON); postRequest.setEntity(bodyEntity); // 发送POST请求 HttpResponse response = HttpClientBuilder.create().build().execute(postRequest); // 解析响应 if (response.getStatusLine().getStatusCode() == 200) { System.out.println("Job submitted successfully!"); } else { System.out.println("Failed to submit job."); } } } ``` 以上代码仅供参考,实际情况可能需要根据不同的Flink版本和RESTful接口进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值