通用查询的抽象 - 孙工的编程生涯
source link: https://www.cnblogs.com/javaddd/p/16461757.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
通用查询的抽象
有一批查询接口,只有参数,返回值,接口地址不通,如何抽象出通用的调用方式呢?
分析一下,每个接口返回类型不一样,我想到了泛型,参数,接口地址不一样,可以通过参数区分。于是先定义一个带泛型的接口
public interface CommonQueryService<T> { /** * 通用查询 * @param requestDto 请求dto * @param apiUrl 请求地址 * @param msg 日志消息表示 * @return 查询返回对象 * @throws InvalidCipherTextException */ GenericReturnObject<List<T>> commonQuery(Object requestDto,String apiUrl, String msg) throws InvalidCipherTextException; }
谁来实现它呢?我定义一个实现类吧
@Slf4j public abstract class CommonQueryServiceImpl<T> implements CommonQueryService<T> { @Autowired private BusinessCenterTokenHelper businessCenterTokenHelper; @Autowired private EpServiceConfig epServiceConfig; protected String returnJson; public GenericReturnObject<List<T>> commonQuery(Object requestDto,String apiUrl,String msg) throws InvalidCipherTextException { String jsonBody = com.alibaba.fastjson.JSON.toJSONString(requestDto); long date = System.currentTimeMillis(); log.info("esServiceConfig:{}", com.alibaba.fastjson.JSON.toJSONString(epServiceConfig)); String appPubKey = epServiceConfig.getApp().getPubkey() ; log.info("appPubKey:{}",appPubKey); String token = businessCenterTokenHelper.getToken(); log.info("token:{}",token); String signature = EncryptUtil.sign("POST" + '\n' + date + '\n'+ token + '\n' + appPubKey +'\n'+ jsonBody); log.info("signature:{}",signature); String dateX = String.valueOf(date); log.info("dataX:{}",dateX); EPRequestCaller caller = null; try { caller = EPRequestCaller.newBuilder(apiUrl) .token(token) .apiVersion(epServiceConfig.getApp().getVersion()) .clientId(epServiceConfig.getClient().getId()) .putHeadParamsMap("x-date", dateX) .putHeadParamsMap("x-token",token) .putHeadParamsMap("x-signature",signature) .appPubKey(appPubKey); caller.setContentBody(jsonBody); this.returnJson = caller.doPost(); GenericReturnObject<List<T>> returnObject = convertJson(returnJson); log.info(">>>>>>>>>>>>>>>>>>>>>>>>>{},返回:{}>>>>>>>>>>>>>>>>>>>>>",msg, com.alibaba.fastjson.JSON.toJSONString(returnObject)); return returnObject; } catch (HttpCallerException e) { log.error("msg:{} 失败:{}",msg,e); GenericReturnObject genericReturnObject = new GenericReturnObject(); genericReturnObject.setErrors(e.getMessage()); return genericReturnObject; } catch (Exception e) { log.error("msg:{}.e:{}",msg,e); GenericReturnObject genericReturnObject = new GenericReturnObject(); genericReturnObject.setErrors(e.getMessage()); return genericReturnObject; } } /** * 中台返回的json转换为json对象,由子类实现 * @param json * @return 返回对象 */ public abstract GenericReturnObject<List<T>> convertJson(String json);
大家看到这是一个抽象类,本来不打算用抽象类的,奈何JSON转对象的时候,不支持泛型的转换,于是定义为抽象类,再定义一个抽象方法 convertJson,把转换的工作交给子类来实现,这样就不涉及JSON转泛型对象的问题了。
对于不同的查询,再定义不用的实现类继承上述抽象类,并实现通用查询接口。下面举2个例子。
@Service("commonQueryServiceFire") public class CommonQueryServiceFireImpl<FireInfoDto> extends CommonQueryServiceImpl<FireInfoDto> implements CommonQueryService<FireInfoDto> { @Override public GenericReturnObject<List<FireInfoDto>> commonQuery(Object requestDto, String apiUrl,String msg) throws InvalidCipherTextException { GenericReturnObject<List<FireInfoDto>> listGenericReturnObject = super.commonQuery(requestDto, apiUrl,msg); return listGenericReturnObject; } @Override public GenericReturnObject<List<FireInfoDto>> convertJson(String json) { GenericReturnObject<List<FireInfoDto>> returnObject = com.alibaba.fastjson.JSON.parseObject(returnJson, new TypeReference<GenericReturnObject<List<FireInfoDto>>>(){}); return returnObject; } }
@Service("commonQueryServiceFireReport") public class CommonQueryServiceFireReport<ForecastReportDto> extends CommonQueryServiceImpl<ForecastReportDto> implements CommonQueryService<ForecastReportDto> { @Override public GenericReturnObject<List<ForecastReportDto>> commonQuery(Object requestDto, String apiUrl,String msg) throws InvalidCipherTextException { GenericReturnObject<List<ForecastReportDto>> listGenericReturnObject = super.commonQuery(requestDto, apiUrl,msg); return listGenericReturnObject; } @Override public GenericReturnObject<List<ForecastReportDto>> convertJson(String json) { GenericReturnObject<List<ForecastReportDto>> returnObject = com.alibaba.fastjson.JSON.parseObject(returnJson, new TypeReference<GenericReturnObject<List<ForecastReportDto>>>(){}); return returnObject; } }
controller调用端
@Resource(name = "commonQueryServiceFire") private CommonQueryService commonQueryServiceFire; @Resource(name = "commonQueryServiceFireReport") private CommonQueryService commonQueryServiceFireReport;
@PostMapping("/environCenter/fire/fireRealData")
public GenericReturnObject fireRealData(String date,
String fireCode ) throws Exception {
FireRealDataReqDto fireRealDataReqDto = new FireRealDataReqDto();
fireRealDataReqDto.setFireCode(fireCode);
fireRealDataReqDto.setDate(date);
GenericReturnObject<List<FireInfoDto>> listGenericReturnObject = commonQueryServiceFire.commonQuery(fireRealDataReqDto,
"/environCenter/fire/fireRealData",
"查询xxx信息");
return listGenericReturnObject;
}
@PostMapping("/environCenter/fire/fireReport")
public GenericReturnObject fireReport(String date ) throws Exception {
date );
FireReportReqDto fireReportReqDto = new FireReportReqDto();
fireReportReqDto.setDate(date);
GenericReturnObject<List<ForecastReportDto>> listGenericReturnObject = commonQueryServiceFireReport.commonQuery(fireReportReqDto,
"/environCenter/fire/fireReport",
"yyy查询");
return listGenericReturnObject;
}
新增不用的查询,需要增加新的实现类,参数dto,返回dto,调用方注入实现类,调用commonQuery方法即可。隐藏了调用细节,不必拷贝重复的代码。(代码删除了注释部分,避免不必要的隐私泄露)。
Recommend
-
111
编程生涯中遇到最厉害的三个技术大牛
-
80
程序员编程生涯中的八大易犯职业错误 你中招了吗?
-
13
在 Astro 钱包 (最近已被 Coinbase 收购)过去两年的大部分时间里,我们都在研究各种形式的经济抽象(economic abstraction)。我们想要和区块链行业中更多的同仁分享我们的发现,希望可以为...
-
17
作者:svenzeng,腾讯 PCG 前端开发工程师 面向抽象编程,是构建一个大型系统非常重要的参考原则。 但对于许多前端同学来说,对面向抽象编程的理解说不上很深刻。大部分同学的习惯是 拿到需求单和设计稿之后就开始编写...
-
7
-
6
大家好,我是龙叔,18年的程序员生涯里,我读过不下200本技术书籍,现在家里的书房还有几十本觉得不错的书籍,经常会拿来翻阅。 一、论看书的重要性 现在不像十几年前那样,现在的学习资源很多,出了校园工作了之后,很多人都不怎么喜欢看书...
-
6
面向对象编程核心是多态!不是抽象哦 | by Sohee Kim 运行良好的软件和运行正常的软件之间是有区别的。仅考虑其行为,构建有效的软件是一项工作完成了一半。软件工程师应该...
-
12
一个简单的 Golang 分页查询通用接口2020-11-18Golang本文介绍了一个简单的 Golang 分页查询通用接口。
-
4
C#面向抽象编程第二讲 抽象编程怎么说呢,以观察者模式为例: 观察者模式有两个对象,...
-
4
面向接口编程实践之aspnetcoreapi的抽象 最为一名越过菜...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK