小說系統原始碼開發,如何優雅的實現對外介面?
做介面需要考慮的問題
package com.caiex.vb.model; import java.io.Serializable;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "Result", propOrder = { "resultCode", "resultMsg" })public class Result implements Serializable { private static final long serialVersionUID = 10L; protected int resultCode; protected String resultMsg; public int getResultCode() { return this.resultCode; } public void setResultCode(int value) { this.resultCode = value; } public String getResultMsg() { return this.resultMsg; } public void setResultMsg(String value) { this.resultMsg = value; }}
package com.caiex.vb.model; import java.io.Serializable; public class Response implements Serializable { private static final long serialVersionUID = 2360867989280235575L; private Result result; private Object data; public Result getResult() { if (this.result == null) { this.result = new Result(); } return result; } public void setResult(Result result) { this.result = result; } public Object getData() { return data; } public void setData(Object data) { this.data = data; } }
reponse code public static int NO_AGENT_RATE = 1119; //未找到兌換率 public static int SCHEME_COMMIT_FAIL = 4000; //方案提交失敗 public static int SCHEME_CONFIRMATION = 4001; //方案確認中 public static int SCHEME_NOT_EXIST = 4002; //方案不存在 public static int SCHEME_CANCEL= 4005; //方案不存在 //。。。。
package com.caiex.vb.interceptor; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody; import com.caiex.vb.model.Response; @ControllerAdvice @ResponseBodypublic class GlobalExceptionHandler { private Logger logger = LoggerFactory.getLogger(this.getClass()); /** * 所有異常報錯 * @param request * @param exception * @return * @throws Exception */ @ExceptionHandler(value=Exception.class) public Response allExceptionHandler(HttpServletRequest request, Exception exception) throws Exception { logger.error("攔截到異常:", exception); Response response = new Response(); response.setData(null); response.getResult().setResultCode(9999); response.getResult().setResultMsg("系統繁忙"); return response; } }
package com.caiex.vb.interceptor; import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.stereotype.Component;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView; import com.alibaba.fastjson.JSON;import com.caiex.redis.service.api.RedisApi;import com.caiex.vb.model.Response;import com.caiex.vb.utils.CaiexCheckUtils; @Componentpublic class SignInterceptor extends BaseValidator implements HandlerInterceptor{ private Logger logger = LogManager.getLogger(this.getClass()); @Resource private RedisApi redisApi; public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception { // TODO Auto-generated method stub } public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception { // TODO Auto-generated method stub } public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception { if(isTestIpAddr(arg0)){ return true; } String securityKey = redisApi.hGet("securityKey", arg0.getParameter("agentid")); if(StringUtils.isEmpty(securityKey)){ Response response = new Response(); response.setData(null); response.getResult().setResultCode(8001); response.getResult().setResultMsg("缺少私鑰, 渠道號:" + arg0.getParameter("agentid")); logger.error("缺少私鑰, 渠道號:" + arg0.getParameter("agentid")); InterceptorResp.printJson(arg1, response); return false; } if(StringUtils.isEmpty(arg0.getParameter("sign")) || !arg0.getParameter("sign").equals(CaiexCheckUtils.getSign(arg0.getParameterMap(), securityKey))){ Response response = new Response(); response.setData(null); response.getResult().setResultCode(3203); response.getResult().setResultMsg("引數簽名認證失敗"); logger.error("引數簽名認證失敗:" + JSON.toJSONString(arg0.getParameterMap()) + " securityKey = " + securityKey); InterceptorResp.printJson(arg1, response); return false; }else{ return true; } } }
package com.caiex.oltp.config; import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import com.caiex.oltp.interceptor.APILimitRateValidator;import com.caiex.oltp.interceptor.CommonValidator;import com.caiex.oltp.interceptor.DDSAuthValidator;import com.caiex.oltp.interceptor.QueryPriceParamsValidator;import com.caiex.oltp.interceptor.TradeParamsValidator; @EnableWebMvc @Configuration @ComponentScanpublic class WebAppConfigurer extends WebMvcConfigurerAdapter { @Bean CommonValidator commonInterceptor() { return new CommonValidator(); } @Bean DDSAuthValidator ddsAuthInterceptor() { return new DDSAuthValidator(); } @Bean QueryPriceParamsValidator queryPriceParamsInterceptor() { return new QueryPriceParamsValidator(); } @Bean TradeParamsValidator tradeParamsInterceptor() { return new TradeParamsValidator(); } @Bean APILimitRateValidator aPILimitRateInterceptor() { return new APILimitRateValidator(); } @Override public void addInterceptors(InterceptorRegistry registry) { //訪問速率限制 registry.addInterceptor(aPILimitRateInterceptor()) .addPathPatterns("/*/*"); //.addPathPatterns("/price/getPriceParam"); //引數簽名認證 registry.addInterceptor(ddsAuthInterceptor()) .addPathPatterns("/tradeState/*") .addPathPatterns("/recycle/*") .addPathPatterns("/matchInfo/*") .addPathPatterns("/price/tradeTicketParam"); //公共引數檢查 registry.addInterceptor(commonInterceptor()) .addPathPatterns("/price/tradeTicketParam") .addPathPatterns("/tradeState/*") .addPathPatterns("/recycle/*"); //詢價引數校驗 registry.addInterceptor(queryPriceParamsInterceptor()) .addPathPatterns("/price/getPriceParam"); //交易引數檢查 registry.addInterceptor(tradeParamsInterceptor()) .addPathPatterns("/price/tradeTicketParam"); super.addInterceptors(registry); }}
private String createToken(){ String utk = "Msk!D*"+System.currentTimeMillis()+"UBR&FLP"; logger.info("create token --- "+Md5Util.md5(utk)); return Md5Util.md5(utk); }
如何提高介面的高併發和高可用
總結
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69996194/viewspace-2844942/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 婚戀app原始碼開發,如何實現介面效能優化?APP原始碼優化
- 有聲小說系統原始碼,小說聽書成品搭建,小說漫畫功能開發原始碼
- 遊戲陪玩系統原始碼開發,如何實現圖片和動畫的優化?遊戲原始碼動畫優化
- 如何開發陪玩系統原始碼的列表頁面,相關實現程式碼原始碼
- 如何優雅的使用介面
- 如何優雅的分析 ThinkPHP 框架原始碼PHP框架原始碼
- 聽書小說APP無加密原始碼開發系統定製功能APP加密原始碼
- 智慧線上客服系統原始碼GOFLY開發日誌-6. gin框架的優雅退出原始碼Go框架
- 一對一直播系統原始碼開發工具如何搭建?原始碼
- 如何實現優雅的重試?
- 說說股票配資系統中實盤交易介面的開發
- 慧優米商城系統開發原始碼部署原始碼
- 【優雅寫程式碼系統】springboot+mybatis+pagehelper+mybatisplus+druid教你如何優雅寫程式碼Spring BootMyBatisUI
- 語音社交app開發,如何實現介面優化?APP優化
- SpringBoot介面 - 如何優雅的對引數進行校驗?Spring Boot
- 量化交易系統開發技術實現方案及原始碼原始碼
- NFT質押交易系統開發功能實現(原始碼示例)原始碼
- 如何TypeScript中相對優雅地實現類的多繼承TypeScript繼承
- 呼叫支付介面,實現直播帶貨系統原始碼的線上支付原始碼
- 期權現貨交易系統開發|秒合約系統開發原始碼原始碼
- 乾貨:如何藉助小程式雲開發實現小程式支付功能(含原始碼)原始碼
- 如何優雅的實現訊息通訊?
- 小說APP原始碼開發,支援二次開發功能APP原始碼
- SOFAJRaft原始碼閱讀-ShutdownHook如何優雅的停機Raft原始碼Hook
- Flutter | 如何優雅的開發一個外掛併發布到Dart倉庫?FlutterDart
- 秒合約系統開發邏輯實現,秒合約系統開發部分原始碼參考原始碼
- 國珍優選系統開發技術原始碼搭建原始碼
- ThinkPHP開發的實用小程式原始碼PHP原始碼
- NFT元宇宙鏈遊系統開發說明(原始碼示例)元宇宙原始碼
- 量化對沖搬磚交易系統開發(開發策略)丨量化對沖搬磚交易系統開發原始碼原始碼
- 一對一直播原始碼對網路教育做出的系統開發原始碼
- 小說系統開發中,值得一看的效能優化重點優化
- PHP外掛系統的實現(五):觸發外掛PHP
- 小說網站製作,小說網站原始碼,小說網站定製開發,小說網站建設網站原始碼
- .Net Core如何優雅的實現中介軟體
- 如何優雅地實現多資料庫的發件箱模式資料庫模式
- 從原始碼分析如何優雅的使用 Kafka 生產者原始碼Kafka
- 量化交易系統開發(說明流程)丨合約量化系統開發(技術分析及原始碼)原始碼