update 发消息使用HttpUtil

This commit is contained in:
lijiarao
2024-05-08 18:04:13 +08:00
parent e388c67aae
commit cec535c623
@@ -1,5 +1,9 @@
package com.jero.modules.docking.hiwork.util; package com.jero.modules.docking.hiwork.util;
import cn.hutool.http.Header;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
@@ -7,6 +11,7 @@ import com.jero.modules.docking.hiwork.entity.HiworkResult;
import com.jero.modules.docking.hiwork.entity.HiworkTodo; import com.jero.modules.docking.hiwork.entity.HiworkTodo;
import com.jero.modules.docking.hiwork.entity.HiworkDingDingMsg; import com.jero.modules.docking.hiwork.entity.HiworkDingDingMsg;
import com.jero.modules.docking.hiwork.entity.HiworkSystemMsg; import com.jero.modules.docking.hiwork.entity.HiworkSystemMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@@ -18,11 +23,14 @@ import org.springframework.web.client.RestTemplate;
import java.util.List; import java.util.List;
import static net.sf.jsqlparser.parser.feature.Feature.execute;
/** /**
* @Author: liao * @Author: liao
* @Date: 2023/10/13 16:55 * @Date: 2023/10/13 16:55
* @Description: Hiwork集成 * @Description: Hiwork集成
*/ */
@Slf4j
@Component @Component
public class HiworkPostUtil { public class HiworkPostUtil {
@Autowired @Autowired
@@ -64,6 +72,7 @@ public class HiworkPostUtil {
* @Description: 远程调用Hiwork * @Description: 远程调用Hiwork
**/ **/
private void postHiwork(String jsonString, String url) { private void postHiwork(String jsonString, String url) {
log.info("远程调用Hiwork,url: {}, body: {}",url,jsonString);
// 设置请求头 // 设置请求头
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
@@ -71,16 +80,19 @@ public class HiworkPostUtil {
HttpEntity<String> requestEntity = new HttpEntity<>(jsonString, headers); HttpEntity<String> requestEntity = new HttpEntity<>(jsonString, headers);
// 发送 POST 请求并获取响应 // 发送 POST 请求并获取响应
ResponseEntity<HiworkResult> response = restTemplate.postForEntity(url, requestEntity, HiworkResult.class); HttpResponse response = HttpUtil.createPost(url)
.header(Header.CONTENT_TYPE, "application/json")
.body(jsonString)
.execute();
// 处理响应数据 // 处理响应数据
if (response.getStatusCode().is2xxSuccessful()) { if (response.isOk()) {
HiworkResult hiworkResult = response.getBody(); String body = response.body();
HiworkResult hiworkResult = JSONObject.parseObject(body, HiworkResult.class);
if (!hiworkResult.getSuccess()) { if (!hiworkResult.getSuccess()) {
throw new JeroBootException("Hiwork:" + hiworkResult.getMsg()); throw new JeroBootException("Hiwork:" + hiworkResult.getMsg());
} }
} else { } else {
throw new JeroBootException("Hiwork: request failed with status code: " + response.getStatusCode()); throw new JeroBootException("Hiwork: request failed with status code: " + response.getStatus());
} }
} }