接收httpPost请求HttpEntity方式
2015-12-25 15:47
701 查看
使用org.springframework.http.HttpEntity 接收数据,方便省事,不用再用流去解析数据,直接看示例:
public void callback(HttpEntity<byte[]> requestEntity, HttpServletResponse response) {
String txNo = UUID.randomUUID();
try{
//直接解析
byte[] body = requestEntity.getBody();
获取数据
String requestJson = new String(body,"UTF-8");
log.info("callback,txNo:{},requestJson :{}",txNo,requestJson);
log.info("fengWo callback decode,txNo:{},requestJson :{}",txNo,URLDecoder.decode(requestJson, "UTF-8"));
//业务代码 不需细看
FengWoCallBack callback = JSON.parseObject(requestJson, FengWoCallBack.class);
output(response, "1");
}
log.info("fengWo callback finished,txNo:{} ,dto:{}", txNo, activeOrderDTO);
} catch (Exception e){
output(response, "1");
log.error("fengWo exception :{} ,teNo{}", ExceptionUtils.getStackTrace(e),txNo);
}
}
//返回
private void output(HttpServletResponse response, String content) {
try {
response.getOutputStream().write(content.getBytes());
response.getOutputStream().flush();
} catch (Exception e) {
log.error("fengWo exception :{}", ExceptionUtils.getStackTrace(e));
}
}
}
public void callback(HttpEntity<byte[]> requestEntity, HttpServletResponse response) {
String txNo = UUID.randomUUID();
try{
//直接解析
byte[] body = requestEntity.getBody();
获取数据
String requestJson = new String(body,"UTF-8");
log.info("callback,txNo:{},requestJson :{}",txNo,requestJson);
log.info("fengWo callback decode,txNo:{},requestJson :{}",txNo,URLDecoder.decode(requestJson, "UTF-8"));
//业务代码 不需细看
FengWoCallBack callback = JSON.parseObject(requestJson, FengWoCallBack.class);
output(response, "1");
}
log.info("fengWo callback finished,txNo:{} ,dto:{}", txNo, activeOrderDTO);
} catch (Exception e){
output(response, "1");
log.error("fengWo exception :{} ,teNo{}", ExceptionUtils.getStackTrace(e),txNo);
}
}
//返回
private void output(HttpServletResponse response, String content) {
try {
response.getOutputStream().write(content.getBytes());
response.getOutputStream().flush();
} catch (Exception e) {
log.error("fengWo exception :{}", ExceptionUtils.getStackTrace(e));
}
}
}
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- GET和post取值限制区别分析
- 探秘ajax跨域请求
- 深入C#中get与set的详解
- POST与GET方法的区别简要分析
- C#使用post发送和接收数据的方法
- PHP响应post请求上传文件的方法
- VB使用XMLHTTP实现Post与Get的方法
- 分享下GET和POST的真正区别
- php采用ajax数据提交post与post常见方法总结
- ajax 的post方法实例(带循环)
- PHP实现支持GET,POST,Multipart/form-data的HTTP请求类
- C#实现的三种模拟自动登录和提交POST信息的方法
- ASP.Net Post方式获取数据流的一种简单写法