项目中,难免会和第三方进行交互,我们请求第三方后经常会遇到第三方回调我们后台,最近也是刚好遇到一些问题,写下来帮助记忆,也希望能够帮助大家!
参数放入URL:
请求方式如:
后台打印:
2018-11-19 13:40:43.477 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the uri is /applyCard/notify
2018-11-19 13:40:43.483 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the client ip is 115.204.197.53
2018-11-19 13:40:43.483 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the method is POST
2018-11-19 13:40:43.483 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the param name is clientNo, the value is 2018102910532600610052
2018-11-19 13:40:43.483 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the param name is tradeNo, the value is 1056740764958064640
2018-11-19 13:40:43.483 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the param name is message, the value is
2018-11-19 13:40:43.484 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the param name is callbackType, the value is CALLBACK_SUCCESS
2018-11-19 13:40:43.484 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the param name is sign, the value is
2018-11-19 13:40:43.484 INFO 28371 --- [nio-80-exec-512] c.n.repayment.filter.ParamInterceptor : the param name is timestamp, the value is
2018-11-19 13:40:43.514 INFO 28371 --- [nio-80-exec-512] c.n.r.controller.ApplyCardController : 回调结果 {tradeNo=1056740764958064640, sign=, clientNo=2018102910532600610052, message=, callbackType=CALLBACK_SUCCESS, timestamp=}
2018-11-19 13:40:43.515 INFO 28371 --- [nio-80-exec-512] c.n.r.controller.ApplyCardController : 状态成功 {tradeNo=1056740764958064640, sign=, clientNo=2018102910532600610052, message=, callbackType=CALLBACK_SUCCESS, timestamp=}
处理方式:
public void notify(HttpServletRequest request, HttpServletResponse response) {
String resultFail = "fail";
String resultSuccess = "success";
- Map<String, String> params = new HashMap<>();
- Enumeration<String> names = request.getParameterNames();
- String key;
- while (names.hasMoreElements()) {
- key = names.nextElement();
- params.put(key, request.getParameter(key));
- }
- logger.info("回调结果 {}", params);
-
- String clientNo = params.get("clientNo");
- String tradeNo = params.get("tradeNo");
- String callbackType = params.get("callbackType");
- String sign = params.get("sign");
- String timestamp = params.get("timestamp");
-
-
- if( "CALLBACK_SUCCESS".equals(callbackType) ){
- logger.info("状态成功 {}", params);
- //成功返回 success\
- responseBody(response, resultSuccess);
- }else {
- logger.info("状态失败{}", params);
- //失败返回 fail\
- responseBody(response, resultFail);
- }
}
/**
* 输出内容到
* @param response 响应对象
* @param contentBody 输出内容
*/
private void responseBody(HttpServletResponse response, String contentBody) {
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("type=text/html; charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write(contentBody);
writer.flush();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
参数放入Body请求:
格式如下:
后台打印:
2018-11-19 14:31:09.454 INFO 8228 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-11-19 14:31:09.542 INFO 8228 --- [nio-8080-exec-1] c.n.repayment.filter.ParamInterceptor : the uri is /applyCard/notify
2018-11-19 14:31:09.542 INFO 8228 --- [nio-8080-exec-1] c.n.repayment.filter.ParamInterceptor : the client ip is 0:0:0:0:0:0:0:1
2018-11-19 14:31:09.543 INFO 8228 --- [nio-8080-exec-1] c.n.repayment.filter.ParamInterceptor : the method is POST
2018-11-19 14:31:09.831 INFO 8228 --- [nio-8080-exec-1] c.n.r.controller.ApplyCardController : 回调结果 {"tradeNo":"123456789","sign":"xxxxx","clientNo":"9018239123801","message":"","callbackType":"CALLBACK_SUCCESS","timestamp":"1529026610813"}
2018-11-19 14:31:09.862 INFO 8228 --- [nio-8080-exec-1] c.n.r.controller.ApplyCardController : 状态成功 {"tradeNo":"123456789","sign":"xxx","clientNo":"9018239123801","message":"","callbackType":"CALLBACK_SUCCESS","timestamp":"1529026610813"}
处理方式:
public void notify(HttpServletRequest request, HttpServletResponse response) {
String resultFail = "fail";
String resultSuccess = "success";
try {
//后台接收
InputStreamReader reader=new InputStreamReader(request.getInputStream(),"UTF-8");
char [] buff=new char[2048];
int length=0;
String result = null;
while((length=reader.read(buff))!=-1){
result = new String(buff,0,length);
}
JSONObject params = JSONObject.parseObject(result);
logger.info("回调结果 {}", params);
String clientNo = (String) params.get("clientNo");
String tradeNo = (String) params.get("tradeNo");
String callbackType = (String) params.get("callbackType");
String sign = (String) params.get("sign");
String timestamp = (String) params.get("timestamp");
if( "CALLBACK_SUCCESS".equals(callbackType) ){
logger.info("状态成功 {}", params);
//成功返回 success
responseBody(response, resultSuccess);
}else {
logger.info("状态失败{}", params);
//失败返回 fail
responseBody(response, resultFail);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
/**
* 输出内容到
* @param response 响应对象
* @param contentBody 输出内容
*/
private void responseBody(HttpServletResponse response, String contentBody) {
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("type=text/html; charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.write(contentBody);
writer.flush();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
以上就是解决方法,如有不懂之处可以留言,