From 3ea10870471929ef2a22313132dc9e835d9b6027 Mon Sep 17 00:00:00 2001 From: jinceon Date: Fri, 14 Jul 2023 06:44:40 +0000 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=94=A8=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E7=9A=84=E5=B7=A5=E5=85=B7=20=E4=B8=8D?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E7=94=A8Feign=E3=80=81RestTemplate=E7=AD=89?= =?UTF-8?q?=E6=B5=81=E8=A1=8C=E7=9A=84http=20client=E3=80=82=E8=A6=81?= =?UTF-8?q?=E7=94=A8=E4=BB=96=E4=BB=AC=E5=B0=81=E8=A3=85=E8=BF=87=E7=9A=84?= =?UTF-8?q?=E8=BF=99=E4=B8=AAchannel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: jinceon --- Java/Channel.java | 85 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Java/Channel.java diff --git a/Java/Channel.java b/Java/Channel.java new file mode 100644 index 0000000..ce40343 --- /dev/null +++ b/Java/Channel.java @@ -0,0 +1,85 @@ +/* +假设要调用一个第三方的接口 +POST /a/b HTTP/1.1 +Host: example.com +Content-Type: application/json +Content-Length: 25 + +{ + "hello":"world" +} + +不允许用Feign、RestTemplate等流行的http client。要用他们封装过的这个channel + +*/ + +@CommandMode(channel = "XXX", channelProduct = "YYY", busiType = "ZZZ") +@StrategyMode(protocol = "http", + method = "POST", + dataType = "json", + urlKey = "xxx.url", // 指向配置文件 xxx.url=https://example.com/a/b + signFied = "sign") // 接口的一些鉴权 +@ResponseMode(responseClass = DemoRespVo.class) // 返回报文的处理 + +@Slf4j +@Data +@Accessors(chain = true) +public class DemoReqVo extends RequestVo { //请求参数的处理 + + private String hello; + + @Override + public RequestVo assignment(Object params, SpringBeanUtil springBeanUtil) { + return super.assignment(params, springBeanUtil); + } + + @Override + public String getInterfaceType() { + return "DEMO"; + } + +} + + +public class DemoRespVo implements ResponseVo { + + + private AAA aaa; //返回报文格式 + + @Override + public void verifySign(ProtocolResponse protocolResponse, StrategyMode strategyMode, SpringBeanUtil springBeanUtil) { + // 校验接口返回的报文签名,如果有的话 + } + + @Override + public ResponseCode convertRespData(ProtocolResponse protocolResponse, StrategyMode strategyMode, SpringBeanUtil springBeanUtil, Class aClass) { + try { + String respBody = protocolResponse.getResponseBody(); + log.info("demo响应报文:{}",respBody); + if(!protocolResponse.isSuccessful()){ + log.error("调用demo通讯异常:{}",protocolResponse.getCode()); + return ResponseCode.build(RespCodeEnum.E0500.getValue(),"调用demo通讯异常",null); + } + // 此处省略若干行 对响应报文的处理 + }catch (Exception e){ + log.error("处理返回报文异常:",e); + return ResponseCode.build(RespCodeEnum.E0500.getValue(),"处理返回报文异常",null); + } + } + + @Override + public String getTradeStatus() { + return null; + } + + @Override + public String getErrMsg() { + return null; + } + +} + + +// 调用的时候,怎样调用呢? +// @CommandMode(channel = "XXX", channelProduct = "YYY", busiType = "ZZZ") +ResponseCode response = channelHandler.channelHandler("XXX", "YYY", "ZZZ", "报文体") \ No newline at end of file -- Gitee