java @rest接口 在网银端调用柜面方法
在企业软件系统中,我们会经常用到柜面和网银两个系统,分别提供给柜员和经销商来操作。而网银端的操作则需经常和柜面端进行联动,此处为通过调用rest接口来实现网银调动柜面操作的讲解。
- 网银端
举例:此处为网银端某一提交方法的部分引用。
public void submit(){
String gmServiceAddress = AppProperties.getProperty("GM_SERVICE_ADDRESS");
String resetAddress = "rest/message/AttachMessageSubmit";
JSONObject data = (JSONObject) WyHttpURLConnectionUtil.postEncryJson(gmServiceAddress, restAddress, params);
}
public static Object postEncryJson(String gmServiceAddress, String resetAddress, Map<String, Object> map) {
String result = null;
HttpURLConnection connection = null;
BufferedInputStream bis = null;
try {
URL url = new URL(gmServiceAddress + resetAddress);
connection = (HttpURLConnection) url.openConnection();
//设置请求头
setHeader(connection);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "UTF-8");
connection.setConnectTimeout(TIMEOUT_TIME);
connection.setReadTimeout(TIMEOUT_TIME);
sendJsonStrEncry(map, connection);
bis = new BufferedInputStream(connection.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
copy(bis, out);
byte[] b = out.toByteArray();
result = new String(b, "UTF-8");
if(isEncrypt()){
result= DES3Util.decryptThreeDESECB(result,getRestMessageEncryptKey());
}
} catch (Exception e) {
log.error("接口调用异常:" + gmServiceAddress + resetAddress, e);
throw new TsException("接口调用异常:" + e.getMessage(), e);
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("关闭Http连接异常:" + gmServiceAddress + resetAddress, e);
throw new TsException("关闭Http连接异常:" + e.getMessage(), e);
}
}
if (connection != null) {
connection.disconnect();
}
}
log.info("outputs:" + result);
Object data = getData(result);
return data;
}
private static void sendJsonStrEncry(Map<String, Object> map, HttpURLConnection connection) {
if (map != null && !map.isEmpty()) {
BufferedOutputStream bos = null;
Object jsonObj = JSONSerializer.toJSON(map);
try {
bos = new BufferedOutputStream(connection.getOutputStream());
String content=jsonObj.toString();
//如果要加密,那么json字符串都需要加密
if (isEncrypt()){
content= DES3Util.encrypt(content,getRestMessageEncryptKey());
}
bos.write(content.getBytes());
bos.flush();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
bos = null;
}
}
}
}
}
write()系列方法进行写操作时并不一定直接将所写的内容写出,而先将需要写出的内容放到输出缓冲区,知道缓冲去满、调用flush()方法刷新流或调用close()方法关闭流时才真正输出。这样处理可以减少实际的写出次数,提高系统效率。如果需要写出的内容立即输出,需要在完成write()方法后调用flush()方法刷新流,否则程序可能不能正常工作。
此处为简单的返回方法,柜面端返回的result应该为空,主要是调用接口使柜面的方法执行。
- 柜面端
@Rest(path = "/message")
public class MessageRest {
private final Logger logger=Logger.getLogger(this.getClass());
MessageFlowService service= BeanFactory.getService(MessageFlowService.class);
private BaseDao dao = BeanFactory.getBean(BaseDao.class,"dataSource");
private static TsBaseJdbcDao tsDao = (TsBaseJdbcDao) TsDaoFactory.getTsBaseDao(null);
@Service(path = "/AttachMessageSubmit")
public Map<String,String> setMessageTips(HttpServletRequest request, HttpServletResponse response){
logger.info("-AttachMessageSubmit-");
Map<String, String> result = new HashMap<String, String>();
String applyId=request.getAttribute("applyId")==null?null:request.getAttribute("applyId").toString();
String messageId=request.getAttribute("msgId")==null?null:request.getAttribute("msgId").toString();
String applyType=request.getAttribute("applyType")==null?null:request.getAttribute("applyType").toString();
String dealOpinion = request.getAttribute("dealOpinion")==null?"":request.getAttribute("dealOpinion").toString();
String dealOper = request.getAttribute("dealOper")==null?"":request.getAttribute("dealOper").toString();
String dealTime = request.getAttribute("dealTime")==null?"":request.getAttribute("dealTime").toString();
String eventId =(String)request.getAttribute("eventId");
if (eventId == null || "".equals(eventId.trim())) {
logger.error("事件ID不能为空");
throw new TsException("事件ID不能为空!!");
}
logger.info("applyId:"+applyId+"-msgId:"+messageId+"-applyType:"+applyType+"--eventId:"+eventId);
//0.根据序列ID判断序列是否存在,如果存在,接口返回提交成功,如果不存在,进入以下逻辑
boolean flag = DataSynchEventInfoDomain.isNotExistEvent(eventId);
if(!flag) {
logger.info("序列已存在,接口返回提交成功。");
return result;
}
//1.开启事务
PlatformTransactionManager transMgr = TransactionFactory.getTransactionManager("dataSource");
TransactionDefinition trans = new DefaultTransactionDefinition();
DefaultTransactionStatus status = (DefaultTransactionStatus) transMgr.getTransaction(trans);
try{
//2.插入序列表
logger.info("--2.待办资料提交接口插入序列表--");
Map map=new HashMap();
map.put("applyId",applyId);
map.put("msgId",messageId);
map.put("applyType",applyType);
map.put("eventId",eventId);
DataSynchEventInfoDomain.createDataSychEvent(eventId,applyId,"1","2","04","rest/message/AttachMessageSubmit",
JSONObject.fromObject(map).toString(),"01");
if ("02".equals(applyType)){
StringBuffer sql=new StringBuffer();
sql.append("UPDATE INDV_MESSAGE_FLOW_INFO SET MSG_STATUS='06',DEAL_OPINION=? ,DEAL_OPER=?,DEAL_TIME=? WHERE MSG_STATUS<>'03' AND MSG_ID=? AND APPLY_ID=?");
dao.executeUpdate(sql.toString(),dealOpinion,dealOper,dealTime,messageId,applyId);
}else {
//5.如果是二次复审消息,发起工作流
//如果是二次复审消息,处理人改成历史审批人
String querySql = "SELECT * FROM INDV_MESSAGE_FLOW_INFO WHERE CREATE_NODE = '01' AND MSG_ID = ?";
Map msgMap = dao.executeQueryOne(querySql, messageId);
if (msgMap != null){
createAndSend(messageId,applyId);
}
//3.将消息的状态改为处理完成,并添加处理意见。
service.setMessageStatus(messageId,applyId,"02",dealOpinion,dealOper,dealTime);
}
//4.将挂起的单据解挂。
dao.executeUpdate("UPDATE LOAN_APPLY SET PROC_STATUS='0',HANG_UP_TYPE=NULL WHERE APPLY_ID = ?",applyId);
//6.提交事务
transMgr.commit(status);
}catch (Exception e){
transMgr.rollback(status);
logger.info(e.getMessage());
throw new TsException("待办资料提交接口异常:" + e.getMessage(), e);
}
return result;
}
}
调用成功的话,直接执行柜面端的setMessageTips方法。