🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 接出调用示例 ## **说明** 通讯前置报文、接出配置完成后,其使用方式主要包含两种情形,一是配置接入,在接入的流程中配置使用接出和报文组包、解包;二是 通过接口的方式在其他业务系统调用前置服务的组包、解包、接出通讯。 因此,使用通讯前置进行接出通讯时有两种配置方案: - 第一:配置一个通讯接入,协议类型可以配置一个最简单的http协议,在该通讯接入有发出接出 - 第二:使用api调用通讯服务的接口进行通讯接出 ## **接口调用** #### **1、接出调用** ``` <pre class="calibre29">``` @<span class="token5">SuppressWarnings</span><span class="token1">(</span><span class="token3">"unchecked"</span><span class="token1">)</span> public Object <span class="token5">P_callExternSys</span><span class="token1">(</span>String sysid<span class="token1">,</span> String trancode<span class="token1">,</span> Object request<span class="token1">)</span> <span class="token1">{</span> Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> mapResp <span class="token">=</span> <span class="token4">null</span><span class="token1">;</span> <span class="token4">try</span> <span class="token1">{</span> Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> mapReq <span class="token">=</span> <span class="token4">new</span> <span class="token5">LinkedHashMap</span><span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"remote"</span><span class="token1">,</span> sysid<span class="token1">)</span><span class="token1">;</span><span class="token6">// YFE接出节点编码</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"transCode"</span><span class="token1">,</span> trancode<span class="token1">)</span><span class="token1">;</span><span class="token6">// 交易码</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"request"</span><span class="token1">,</span> request<span class="token1">)</span><span class="token1">;</span><span class="token6">// 参数</span> <span class="token6">// 可以直接使用spring ioc注入</span> RestTemplate restTemplate <span class="token">=</span> SpringContextHolder<span class="token1">.</span><span class="token5">getBean</span><span class="token1">(</span><span class="token3">"balancedTemplate"</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">// yfe的接出调用地址、需要注意是使用IP还是服务名调用</span> String strUrl <span class="token">=</span> <span class="token3">"http://sca-commservice/yfasvc/yfeTransProc"</span><span class="token1">;</span> mapResp <span class="token">=</span> restTemplate<span class="token1">.</span><span class="token5">postForObject</span><span class="token1">(</span>strUrl<span class="token1">,</span> mapReq<span class="token1">,</span> Map<span class="token1">.</span>class<span class="token1">)</span><span class="token1">;</span> <span class="token4">return</span> mapResp<span class="token1">;</span> <span class="token1">}</span> <span class="token4">catch</span> <span class="token1">(</span><span class="token5">Exception</span> ex<span class="token1">)</span> <span class="token1">{</span> log<span class="token1">.</span><span class="token5">error</span><span class="token1">(</span><span class="token3">"error:"</span><span class="token1">,</span> ex<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token4">return</span> mapResp<span class="token1">;</span> <span class="token1">}</span> ``` ``` #### **2、解包调用** ``` <pre class="calibre29">``` @<span class="token5">SuppressWarnings</span><span class="token1">(</span><span class="token3">"unchecked"</span><span class="token1">)</span> public Object <span class="token5">P_yfeUnpack</span><span class="token1">(</span>String system<span class="token1">,</span> String tardeNo<span class="token1">,</span> String tag<span class="token1">)</span> <span class="token1">{</span> Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> mapResp <span class="token">=</span> <span class="token4">null</span><span class="token1">;</span> <span class="token4">try</span> <span class="token1">{</span> Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> mapReq <span class="token">=</span> <span class="token4">new</span> <span class="token5">LinkedHashMap</span><span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"system"</span><span class="token1">,</span> system<span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"transCode"</span><span class="token1">,</span> tardeNo<span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"request"</span><span class="token1">,</span> tag<span class="token1">)</span><span class="token1">;</span> RestTemplate restTemplate <span class="token">=</span> SpringContextHolder<span class="token1">.</span><span class="token5">getBean</span><span class="token1">(</span><span class="token3">"balancedTemplate"</span><span class="token1">)</span><span class="token1">;</span> String strUrl <span class="token">=</span> <span class="token3">"http://sca-commservice/yfasvc/yfeUnPackMsg"</span><span class="token1">;</span> mapResp <span class="token">=</span> restTemplate<span class="token1">.</span><span class="token5">postForObject</span><span class="token1">(</span>strUrl<span class="token1">,</span> mapReq<span class="token1">,</span> Map<span class="token1">.</span>class<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token4">catch</span> <span class="token1">(</span><span class="token5">Exception</span> e<span class="token1">)</span> <span class="token1">{</span> log<span class="token1">.</span><span class="token5">error</span><span class="token1">(</span><span class="token3">"error:"</span><span class="token1">,</span> e<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token4">return</span> mapResp<span class="token1">;</span> <span class="token1">}</span> ``` ``` #### **3、组包调用** ``` <pre class="calibre31">``` public Object <span class="token5">P_yfePack</span><span class="token1">(</span>String system<span class="token1">,</span> String tardeNo<span class="token1">,</span> Map kevalue<span class="token1">)</span> <span class="token1">{</span> Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> mapReq <span class="token">=</span> <span class="token4">new</span> <span class="token5">LinkedHashMap</span><span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"system"</span><span class="token1">,</span> system<span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"transCode"</span><span class="token1">,</span> tardeNo<span class="token1">)</span><span class="token1">;</span> mapReq<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"request"</span><span class="token1">,</span> kevalue<span class="token1">)</span><span class="token1">;</span> String respStr <span class="token">=</span> <span class="token3">""</span><span class="token1">;</span> <span class="token4">try</span> <span class="token1">{</span> RestTemplate restTemplate <span class="token">=</span> SpringContextHolder<span class="token1">.</span><span class="token5">getBean</span><span class="token1">(</span><span class="token3">"balancedTemplate"</span><span class="token1">)</span><span class="token1">;</span> String strUrl <span class="token">=</span> <span class="token3">"http://sca-commservice/yfasvc/yfePackMsg"</span><span class="token1">;</span> respStr <span class="token">=</span> restTemplate<span class="token1">.</span><span class="token5">postForObject</span><span class="token1">(</span>strUrl<span class="token1">,</span> mapReq<span class="token1">,</span> String<span class="token1">.</span>class<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token4">catch</span> <span class="token1">(</span><span class="token5">Exception</span> e<span class="token1">)</span> <span class="token1">{</span> log<span class="token1">.</span><span class="token5">error</span><span class="token1">(</span><span class="token3">"error:"</span><span class="token1">,</span> e<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token4">return</span> respStr<span class="token1">;</span> <span class="token1">}</span> ``` ```