java 接口请求返回通用json
public class BaseResponseBody {
// 不能添加属性 仅做泛型使用
}
public class ResponseBase<T extends BaseResponseBody> {
private Integer msgCode ;
private String msgString;
private long serverTime;
private T body;
public ResponseBase(){
super();
this.msgCode = Integer.parseInt(MsgConstants.RESULT_200[0]);
this.msgString = MsgConstants.RESULT_200[1];
this.serverTime = System.currentTimeMillis();
}
public ResponseBase(T body){
this();
this.body = body;
}
public ResponseBase(String msgCode, String msgString){
super();
if(NumberUtils.isDigits(msgCode)){
this.msgCode = Integer.parseInt(msgCode);
}else{
this.msgCode = Integer.parseInt(MsgConstants.RESULT_502[0]);
}
this.msgString = msgString;
this.serverTime = System.currentTimeMillis();
}
public Integer getMsgCode() {
return msgCode;
}
public String getMsgString() {
return msgString;
}
public long getServerTime() {
return serverTime;
}
public T getBody() {
return body;
}
public void setMsgCode(Integer msgCode) {
this.msgCode = msgCode;
}
public void setMsgString(String msgString) {
this.msgString = msgString;
}
public void setServerTime(long serverTime) {
this.serverTime = serverTime;
}
public void setBody(T body) {
this.body = body;
}
}
public class MsgConstants {
/******************** 平台调用返回失败返回代码和异常描述 ***********************/
public final static String[] RESULT_200 = { "200", "SUCCESS" };
}
@RequestMapping(value = "/contlistAES", method = RequestMethod.POST)
@ResponseBody
public void contlistAES(HttpServletRequest req, HttpServletResponse rsp) {
ContactListRequest request = null;
ResponseBase<SubNodesResp> response = null;
try {
// 解析请求参数并解密
String jsonParams=AppInterFaceUtil.getInputStreamParameter(req);
request = JSON.parseObject(jsonParams, ContactListRequest.class);
if (null==request) {
throw new BaseException(MsgConstants.RESULT_403[0], MsgConstants.RESULT_403[1]);
}
response = new ResponseBase<SubNodesResp>();
//查询数据
SubNodesResp info = departmentService.getChildFromClient(request.getDeptId(),request.getTs());
response.setBody(info);
} catch (BaseException e) {
response = new ResponseBase<SubNodesResp>(e.getCode(), e.getMessage());
} catch (Exception e) {
if (StringUtils.isBlank(e.getMessage())) {
response = new ResponseBase<SubNodesResp>(MsgConstants.RESULT_502[0], MsgConstants.RESULT_502[1]);
} else {
response = new ResponseBase<SubNodesResp>(MsgConstants.RESULT_502[0], e.getMessage());
}
e.printStackTrace();
}
//返回数据
String data = JSON.toJSONString(response, SerializerFeature.WriteMapNullValue);
AjaxUtil.writeClient(data, req, rsp);
}
public class AjaxUtil {
private static Logger logger = LoggerFactory.getLogger(AjaxUtil.class);
/**
* ajax 请求返回的数据
* @param data
* @param req
* @param resp
*/
public static void sendMessage(String data,HttpServletResponse resp){
PrintWriter printWriter = null;
try {
printWriter = resp.getWriter();
printWriter.print(data);
} catch (IOException ex) {
logger.error("Ajax send "+ data + " , Exception :" + ex);
} finally {
if (null != printWriter) {
printWriter.flush();
printWriter.close();
}
}
}
/**
* 封装easyui表格数据
* @param list 列表数据
* @param total 页数
* @return
*/
public static Map<String, Object> getPageListDate(List<?> list,int total){
if (list != null) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("total", total);
result.put("rows", list);
return result;// 这个就是你在ajax成功的时候返回的数据,我在那边进行了一个对象封装
}
return null;
}
/**
* ajax 请求返回的数据
* @param data
* @param req
* @param resp
*/
public static void writeMessage(String data,HttpServletResponse resp){
resp.setHeader("Cache-Control", "no-cache");
resp.setContentType("text/json;charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
PrintWriter printWriter = null;
try {
printWriter = resp.getWriter();
printWriter.write(data);
} catch (IOException ex) {
logger.error("Ajax send "+ data + " , Exception :" + ex);
} finally {
if (null != printWriter) {
printWriter.flush();
printWriter.close();
}
}
}
/**
* ajax 请求返回的数据
* @param data
* @param req
* @param resp
*/
public static void writeClient(String data,HttpServletRequest request,HttpServletResponse response){
PrintWriter printWriter = null;
try {
if(request.getRequestURI().endsWith("AES")){
data = AESUtil.encrypt(data);
}
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
printWriter = response.getWriter();
printWriter.write(data);
} catch (IOException ex) {
logger.error("Ajax send "+ data + " , Exception :" + ex);
} finally {
if (null != printWriter) {
//printWriter.flush();
//printWriter.close();
}
}
}
}
java 接口请求返回通用json的更多相关文章
- NodeJs本地搭建服务器,模拟接口请求,获取json数据
最近在学习Node.js,虽然就感觉学了点皮毛,感觉这个语言还不错,并且也会一步步慢慢的学着的,这里实现下NodeJs本地搭建服务器,模拟接口请求,获取json数据. 具体的使用我就不写了,这个博客写 ...
- 调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错
调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错 在需要解析的类型类上加上声明 eg:
- charles_01_打断点修改接口请求&返回数据
前言 测试过程中,为了模拟某场景测试,经常需要修改接口请求或者返回数据.可通过抓包工具打断点,实现模拟测试场景.常用的抓包工具charles和fiddler均可打断点mock数据.由于小编安装了cha ...
- 使用BeanShell断言判断请求返回的Json相应结果(不同json格式整理)
第一种json格式 { "code": 0, "msg": "success", "success": true, &q ...
- 通过 Ajax 调取后台接口将返回的 json 数据绑定在页面上
第一步: 编写基础的 html 框架内容,并引入 jquery: <!doctype html> <html lang="en"> <head> ...
- C# 请求Web Api 接口,返回的json数据直接反序列化为实体类
须要的引用的dll类: Newtonsoft.Json.dll.System.Net.Http.dll.System.Net.Http.Formatting.dll Web Api接口为GET形式: ...
- PHP拿到接口数据返回的json以及传参-----ajax 跨域请求 ---
以下测试------ <php $ch = curl_init(); $str = '';//此处为接口地址以及传参------- curl_setopt($ch, CURLOPT_URL, $ ...
- 返回通知 对方法返回的结果可以进行加工 例如请求接口后 返回的json参数可以加工成对象返回给调用者
- 【Jmeter测试】如何使用BeanShell断言判断请求返回的Json相应结果
脚本结构上图中,queryMaterialApiDTOListByPkIds是返回Json格式响应结果的请求,然后添加BeanShell断言详细判断Json结果中的值是否正确. Json格式的相 ...
随机推荐
- struts标签错误:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
今天使用eclipse开发ssh,出现Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/co ...
- C++ string的那些坑
1. size_type find_first_of( const basic_string &str, size_type index = 0 ); 查找在字符串中第一个与str中的某个字符 ...
- NOIP模拟4
期望得分:20+100+100=220 实际得分:20+100+100=220 特判相离.内含 对于两圆相交的情况,一直在考虑求交点 实际上相交的面积可以用两个扇形减去两个三角形 正弦定理.余弦定理来 ...
- vim如何粘贴
刚开始vim的学习之旅,在进行粘贴操作时遇到了麻烦. 教程上粘贴的命令是p,但我发现该命令只适用于从vim到vim的复制(p未必就仅仅局限于此,但我目前所了解到的情况如此),当我从网上复制了一段代码想 ...
- CSS 颜色属性
一.十六进制表示,如: a{ color: #F0E68C; } 当RGB一样时,可以简写,如#AABBCC 可以写成 #ABC 二.rgb(r,g,b)形式,其中rgb的值为1~255之间,如: a ...
- 您是哪个等级的CSS开发人员?
我们在不断的学习,追求进步与提高,到底学到什么程度了,到底是 不是真的了解CSS,是哪个层次了呢.我们来对照一下. 第0级:CSS?那不是一个多人射击游戏吗? CSS? Isn't that a m ...
- 铺地砖|状压DP练习
有一个N*M(N<=5,M<=1000)的棋盘,现在有1*2及2*1的小木块无数个,要盖满整个棋盘,有多少种方式?答案只需要mod1,000,000,007即可. //我也不知道这道题的来 ...
- 【Linux 命令】fping ping 包间隔时间详解
服务器间检查会用到fping的命令,期间遇到了一个问题,需要将ping包间的间隔时间设置为100毫秒,查看fping -h看下,找到了-i和-p两个参数: 看到这两个参数,我当时的表情是这样的: 看不 ...
- attachEvent 中this指向
IE中使用的事件绑定函数与Web标准的不同,而且this指向也不一样,Web标签中的this指向与传统事件绑定中的this一样,是当前目标,但是IE中事件绑定函数中this指向,通过使用call或ap ...
- Perl6多线程3: Promise start / in / await
创建一个Promise 并自动运行: my $p = Promise.start({say 'Hello, Promise!'}); 如果把代码改成如下, 我们会发现什么也没打印: ;say 'Hel ...