fix: restTemplate请求添加try-catch

This commit is contained in:
Lesan 2025-01-09 09:36:44 +08:00
parent 533e5c3bf5
commit f1296c59eb
1 changed files with 18 additions and 9 deletions

View File

@ -22,6 +22,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.List; import java.util.List;
@ -79,15 +80,23 @@ public class BpmUserTaskListener implements TaskListener {
// 3. 异步发起请求 // 3. 异步发起请求
// TODO @芋艿确认要同步还是异步 // TODO @芋艿确认要同步还是异步
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers); HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(body, headers);
// TODO @lesan可能需要 try catch RestClientException try {
ResponseEntity<String> responseEntity = restTemplate.exchange(listenerHandler.getPath(), HttpMethod.POST, ResponseEntity<String> responseEntity = restTemplate.exchange(listenerHandler.getPath(), HttpMethod.POST,
requestEntity, String.class); requestEntity, String.class);
log.info("[notify][监听器:{},事件类型:{},请求头:{},请求体:{},响应结果:{}]", log.info("[notify][监听器:{},事件类型:{},请求头:{},请求体:{},响应结果:{}]",
DELEGATE_EXPRESSION, DELEGATE_EXPRESSION,
delegateTask.getEventName(), delegateTask.getEventName(),
headers, headers,
body, body,
responseEntity); responseEntity);
} catch (RestClientException e) {
log.error("[error][监听器:{},事件类型:{},请求头:{},请求体:{},请求出错:{}]",
DELEGATE_EXPRESSION,
delegateTask.getEventName(),
headers,
body,
e.getMessage());
}
// 4. 是否需要后续操作TODO 芋艿待定 // 4. 是否需要后续操作TODO 芋艿待定
} }