import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.customcode.util.FileStreamUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import weaver.conn.RecordSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 接收"杰"提供的数据
* 调用"平"的API传递并接收返回数据(员工入职)
*
* @param request HttpServletRequest 对象
* @param response HttpServletResponse 对象
* @return String
*/
@POST
@Path("/getDocJson")
@Produces({MediaType.APPLICATION_JSON})
public String getDocReadLog(@Context HttpServletRequest request, @Context HttpServletResponse response) {
//获取前端数据
Map<String, Object> map = ParamUtil.request2Map(request);
HashMap<Object, Object> hashMap = new HashMap<>();
hashMap.put("folderPath", map.get("folderPath"));
hashMap.put("account", map.get("account"));
try {
//获取接口的 ip地址
// String localIp = CusUtil.getLocalIp();
// String apiURL = localIp + "3000/createPerson";
String apiURL ="http://192.168.0.191:3000/createPerson"; // 接口的URL
// 创建URL对象
URL url = new URL(apiURL);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法(POST)
connection.setRequestMethod("POST");
// 设置请求头
connection.setRequestProperty("Content-Type", "application/json");
// 启用输出流,用于发送数据
connection.setDoOutput(true);
// 获取输出流并写入JSON数据
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
// 将Map转换为JSON字符串
ObjectMapper objectMapper = new ObjectMapper();
String jsonData1 = objectMapper.writeValueAsString(hashMap);
outputStream.write(jsonData1.getBytes("UTF-8"));
outputStream.flush();
outputStream.close();
// 获取响应状态码
int responseCode = connection.getResponseCode();
// 获取输入流
BufferedReader reader;
if (responseCode >= HttpURLConnection.HTTP_OK && responseCode < HttpURLConnection.HTTP_BAD_REQUEST) {
// 请求成功或者客户端错误
InputStream inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
} else {
// 服务端错误
InputStream errorStream = connection.getErrorStream();
reader = new BufferedReader(new InputStreamReader(errorStream, "UTF-8"));
}
// 读取响应内容
StringBuilder responses = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responses.append(line);
}
reader.close();
//拼接返回参数
Map<String, Object> map1 = new HashMap<>();
//转换map
String s = responses.toString();
Map<String, String> read = objectMapper.readValue(s, Map.class);
// 输出响应状态码和内容
map1.put("code", read.get("code"));
map1.put("msg", read.get("msg"));
map1.put("Response Code:", responseCode);
// 关闭连接
connection.disconnect();
return JSONObject.toJSONString(map1);
} catch (IOException e) {
Map<String, Object> map2 = new HashMap<>();
map2.put("msg", "连接异常");
map2.put("code", "0");
return JSONObject.toJSONString(map2);
}
}
Java代码实现 调用第三方API并获取返回参数
最新推荐文章于 2024-07-17 16:07:45 发布