传入参数有url json对象和请求头,返回的是响应的状态码,可以根据你自己的逻辑改成返回是相应内容
public static int patch(String url,JSONObject jsonParam, Map<String, String> header){
int statusCode = 0;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPatch httpPatch = new HttpPatch(url);
//请求头
if (MapUtils.isNotEmpty(header)) {
for (Map.Entry<String, String> entry : header.entrySet()) {
httpPatch.addHeader(entry.getKey(), entry.getValue());
}
}
httpPatch.setHeader("Content-type", "application/json");
try {
if (jsonParam != null){
StringEntity entity = new StringEntity(jsonParam.toString(),StandardCharsets.UTF_8);
httpPatch.setEntity(entity);
}
HttpResponse response = httpClient.execute(httpPatch);
statusCode = response.getStatusLine().getStatusCode();
} catch (IOException e) {
log.error(e.getMessage());
}finally {
try {
httpClient.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
return statusCode;
}