| package com.dy.rtuMw.web.webRequest; | 
|   | 
| import com.dy.common.contant.Constant; | 
| import com.dy.common.mw.protocol.Command; | 
| import com.dy.common.webUtil.BaseResponse; | 
| import com.dy.rtuMw.server.ServerProperties; | 
| import org.apache.logging.log4j.LogManager; | 
| import org.apache.logging.log4j.Logger; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.http.*; | 
| import org.springframework.http.converter.StringHttpMessageConverter; | 
| import org.springframework.stereotype.Component; | 
| import org.springframework.web.client.RestTemplate; | 
| import org.springframework.web.util.UriComponentsBuilder; | 
|   | 
| import java.nio.charset.StandardCharsets; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2024/11/11 16:29 | 
|  * @Description 通信中间件对外发出的web请求 | 
|  */ | 
| @Component() | 
| public class WebRequestDeal { | 
|   | 
|     private static final Logger log = LogManager.getLogger(WebRequestDeal.class.getName()); | 
|   | 
|     private RestTemplate restTemplate; | 
|   | 
|     @Autowired | 
|     public void setRestTemplate(RestTemplate restTemplate){ | 
|         this.restTemplate = restTemplate ; | 
|     } | 
|   | 
|     /** | 
|      * 处理中间件主动发起的web请求 | 
|      * @param webUrl | 
|      * @param obj | 
|      */ | 
|     public void deal(String webUrl, Object obj) { | 
|         if (webUrl != null | 
|                 && !webUrl.trim().equals("") | 
|                 && !webUrl.trim().equals(Command.ignoreRtuResultSendWebUrl)) { | 
|             String url = UriComponentsBuilder.fromUriString(webUrl) | 
|                     .build() | 
|                     .toUriString(); | 
|             restTemplate.getMessageConverters().set(1,new StringHttpMessageConverter(StandardCharsets.UTF_8)); | 
|             HttpHeaders headers = new HttpHeaders(); | 
|             headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); | 
|             headers.set(Constant.UserTokenKeyInHeader, ServerProperties.orgTag); | 
|             HttpEntity<?> httpEntity = new HttpEntity<>(obj, headers); | 
|             ResponseEntity<BaseResponse> response = null; | 
|             try { | 
|                 // 通过Post方式调用接口 | 
|                 response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); | 
|             } catch (Exception e) { | 
|                 log.error("web访问请求发生异常", e); | 
|             } | 
|             //assert response != null; | 
|         } else { | 
|             log.error("严重错误,web访问的URL为空"); | 
|         } | 
|     } | 
| } |