Solution/ 解决方案

解决方案分类

如何通过Servlet方式调用专业风控接口

发布时间:2020-03-20 点击数:821

1、业务工程引入

     需引入外部jar包,包名:fastjson-1.2.31.jar,httpclient-4.5.jar , httpcore-4.4.1.jar , commons-logging-1.2.jar

2、然后实现,代码示例(Java)如下:

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
* 先导入到工程,需要jar包:fastjson-1.2.31.jar , httpclient-4.5.jar , httpcore-4.4.1.jar , commons-logging-1.2.jar
*
* @author admin
*
*/
public class ServletClientPAY_EVENT {


/**
* 通知方式(status=1 or status=-1)
*/
public static HashMap<String, Object> getAsyncParamsMap(HashMap<String, Object> map, String status) {

map.put("finish_time", getDateTime());
map.put("status", status);
return map;
}

/**
* 请求方式(status=0)
*/
public static HashMap<String, Object> getSyncParamsMap() {

HashMap<String, Object> map = new HashMap<String, Object>();
// 封装事件类型以及事件类型中的字段值
map.put("EVENT_TYPE", "PAY_EVENT");// REQUIRED
map.put("client_ip", "189.219.229.131");

map.put("client_mobile", "13103337128");
map.put("device_id", "70010000001");
map.put("occur_time", getDateTime());// 必填
map.put("payee_userid", "70010000001");

map.put("session_id", "70010000001");
map.put("status","0");// 必填
map.put("user_id", "70010000001");


return map;
}

private static String getDateTime() {
Date date = new Date();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);
}

// ----ServletClient内部类,实际使用可以提取到外面成为公共类--------------------------------------------
public static class ServletClient {

static CloseableHttpClient httpclient = HttpClients.createDefault();

public static String execRequest(String url, Map<String, Object> paramsMap) {
String result = "";
CloseableHttpResponse response = null;
try {
// 创建默认的httpClient实例.
HttpPost httppost = new HttpPost(url);

List formparams = new ArrayList();
for (String key : paramsMap.keySet()) {
formparams.add(new BasicNameValuePair(key, (String) paramsMap.get(key)));
}
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭连接,释放资源
if (response != null)

response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}

public static void closeHttpClient() {
try {
if (httpclient != null)
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// -----------------------------------main---------------------------

public static void main(String[] args) {

String url = "http://192.168.5.7:8099/ReceiveOrderServlet";
int num = 100;//测试循环次数
for(int i = 0; i< num;i++){

HashMap<String, Object> paramsMap = getSyncParamsMap();
String result = ServletClient.execRequest(url, paramsMap);
// json 解释
JSONObject jsonObject = JSON.parseObject(result);

System.out.println("success(调用接口是否成功,true成功,false失败): " + jsonObject.get("success"));
System.out.println("reasonCode(调用失败原因码,参见原因码表): " + jsonObject.get("reasonCode"));
System.out.println("reasonMsg(调用失败原因描述): " + jsonObject.get("reasonMsg"));
System.out.println("orderNo(订单号); " + jsonObject.get("orderNo"));
System.out.println("riskResult(风险评定指令码,有三个,放行(ACCEPT)、待审核(REVIEW)、拒绝(REJECT)): " + jsonObject.get("riskResult"));
System.out.println("riskScore(事件总体评分): " + jsonObject.get("riskScore"));
System.out.println("costTime(处理花费时间(毫秒)): " + jsonObject.get("costTime"));
System.out.println("fireRules(触发的规则列表): " + jsonObject.get("fireRules"));
// fireRules: 触发的规则列表
if (jsonObject.get("fireRules") != null && !"".equals(jsonObject.get("fireRules"))) {

JSONArray fireRules = (JSONArray) jsonObject.get("fireRules");
if (fireRules != null && fireRules.size() > 0) {
System.out.println("---------" + "fireRules: 触发的规则列表" + "-----------------");
for (Object object : fireRules) {
JSONObject ruleJson = JSON.parseObject(object.toString());
System.out.println("code(规则编码): " + ruleJson.get("code"));
System.out.println("name(规则名称): " + ruleJson.get("name"));
System.out.println("isPolicy(预警决策(0,决策;1 预警)): " + ruleJson.get("isPolicy"));
System.out.println("ruleResult(风控结果,单个规则的风控结果。10000(放行),30000(待审核),99999(拒绝)): " + ruleJson.get("ruleResult"));
System.out.println("ruleScore(风控评分,单个规则的风控评分): " + ruleJson.get("ruleScore"));
}
}

}

// 客户根据需要判断
if ("ACCEPT".equals(jsonObject.get("riskResult"))) {

paramsMap = getAsyncParamsMap(paramsMap,"1");
String sucResult = ServletClient.execRequest(url, paramsMap);
System.out.println(sucResult);
} else {
paramsMap = getAsyncParamsMap(paramsMap,"-1");
String failResult = ServletClient.execRequest(url, paramsMap);
System.out.println(failResult);
}
}
}

}

3、对接需要注意如下:

1)  接口参数EVENT_TYPEstatusoccur_time必填;

2)  status1(成功)或-1(失败)时,此字段finish_time必填;

3)  occur_timefinish_time日期格式“yyyy-MM-dd HH:mm:ss.SSS”;

4)  请求与通知不同点:不同在通知时status1-1,必须有finish_time;