public static HttpEntity sendPatch(String url, JSONObject jsonObject, HttpHeaders headers) {
try {
//创建 restTemplate 对象
RestTemplate restTemplatePatch = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
//封装请求内容
HttpEntity<JSONObject> requestEntity = new HttpEntity(jsonObject,headers);
ResponseEntity responseEntity = restTemplatePatch.exchange(url.toString(), HttpMethod.PATCH, requestEntity, Object.class);
log.info("HttpEntity---Patch-" + responseEntity);
return responseEntity;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
请求样例:
public static void main(String arg[]) throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
headers.add("charset","utf-8");
headers.add("Authorization","Bearer "+"");
String syncUserUrl ="";
JSONObject jsonObject = new JSONObject();
jsonObject.put("fail_reason","当前 HC 不可用");
jsonObject.put("redirect_url","https://example.com");
jsonObject.put("state",2);
HttpRequestUtil.sendPatch(syncUserUrl, jsonObject, headers);
}