Spring Boot调用api patch接口
Spring Boot调用api patch接口
示例:
/** * 调用api patch接口 * * @param url * @param jsonArray * @return java.lang.String * @author li.zhm * @date 2022/8/20 14:17 * @history <author> <time> <version> <desc> */ public static String sendPatch(String url, JSONArray jsonArray) { log.info("请求patch地址:{}", url); String body = ""; // 创建httpclient对象 CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = null; // 创建patch方式请求对象 HttpPatch httpPatch = new HttpPatch(url); RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000) .setConnectionRequestTimeout(35000).setSocketTimeout(60000).build(); httpPatch.setConfig(requestConfig); // 装填参数 StringEntity s = new StringEntity(jsonArray.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json-patch+json")); // 设置参数到请求对象中 httpPatch.setEntity(s); log.info("请求参数:{}", jsonArray.toString()); // 设置header信息, 指定报文头【Content-type】 【User-Agent】 httpPatch.setHeader("Content-type", "application/json-patch+json"); httpPatch.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); try { // 执行请求操作,并拿到结果(同步阻塞) response = client.execute(httpPatch); log.info("response 返回状态码:{}", response.getStatusLine().getStatusCode()); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK || response.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) { // 获取结果实体 HttpEntity entity = response.getEntity(); if (entity != null) { // 按指定编码转换结果实体为String类型 body = EntityUtils.toString(entity, "utf-8"); log.info("响应body:", body); } // 关闭流 EntityUtils.consume(entity); } else { log.error("response 返回状态码异常:{}", response.getStatusLine().getStatusCode()); } // 释放链接 response.close(); } catch (IOException e) { log.error("调用amf-update sub api异常,{0}", e); e.printStackTrace(); } finally { if (client != null) { try { client.close(); } catch (IOException e) { log.error("关闭http client异常,{0}", e); e.printStackTrace(); } } if (response != null) { try { response.close(); } catch (IOException e) { log.error("关闭http response异常,{0}", e); e.printStackTrace(); } } } return body; }
public static String sendPatch(String url, JSONArray jsonArray) {
分类:
Java技术
, Sprint Boot
标签:
Java
, Spring Boot
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南