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的更多相关文章

  1. NodeJs本地搭建服务器,模拟接口请求,获取json数据

    最近在学习Node.js,虽然就感觉学了点皮毛,感觉这个语言还不错,并且也会一步步慢慢的学着的,这里实现下NodeJs本地搭建服务器,模拟接口请求,获取json数据. 具体的使用我就不写了,这个博客写 ...

  2. 调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错

    调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错 在需要解析的类型类上加上声明 eg:

  3. charles_01_打断点修改接口请求&返回数据

    前言 测试过程中,为了模拟某场景测试,经常需要修改接口请求或者返回数据.可通过抓包工具打断点,实现模拟测试场景.常用的抓包工具charles和fiddler均可打断点mock数据.由于小编安装了cha ...

  4. 使用BeanShell断言判断请求返回的Json相应结果(不同json格式整理)

    第一种json格式 { "code": 0, "msg": "success", "success": true, &q ...

  5. 通过 Ajax 调取后台接口将返回的 json 数据绑定在页面上

    第一步: 编写基础的 html 框架内容,并引入 jquery: <!doctype html> <html lang="en"> <head> ...

  6. C# 请求Web Api 接口,返回的json数据直接反序列化为实体类

    须要的引用的dll类: Newtonsoft.Json.dll.System.Net.Http.dll.System.Net.Http.Formatting.dll Web Api接口为GET形式: ...

  7. PHP拿到接口数据返回的json以及传参-----ajax 跨域请求 ---

    以下测试------ <php $ch = curl_init(); $str = '';//此处为接口地址以及传参------- curl_setopt($ch, CURLOPT_URL, $ ...

  8. 返回通知 对方法返回的结果可以进行加工 例如请求接口后 返回的json参数可以加工成对象返回给调用者

  9. 【Jmeter测试】如何使用BeanShell断言判断请求返回的Json相应结果

      脚本结构​上图中,queryMaterialApiDTOListByPkIds是返回Json格式响应结果的请求,然后添加BeanShell断言详细判断Json结果中的值是否正确. Json格式的相 ...

随机推荐

  1. 使用uiautomator时遇到问题的处理方法

    本帖持续更新中… 一.使用adb devices无法连接到模拟器 这种情况可能是因为服务挂了之类的原因,重启一下服务 adb kill-server //关闭adb服务 adb start-serve ...

  2. WPF系列之一:基于并行任务和MVVM创建响应灵敏和数据驱动的UI

    在利用WPF创建桌面应用程序的界面时,经常使用MVVM的设计模式,以减少UI层与逻辑层的代码耦合度. 在MVVM的设计中,最主要的方法和技术是UI中的控件利用Binding来和逻辑层(ViewMode ...

  3. 「Linux」centos7安装python

    •安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqli ...

  4. python shell

    os.system(cmd) 命令执行结果 0或者1 output = os.popen(cmd) print output.read() 通过 os.popen() 返回的是 file read 的 ...

  5. u3d摄像机截图

    using System; using UnityEngine; using System.Collections; public class TestCamreaCapture1 : MonoBeh ...

  6. Ubuntu 火狐浏览器中,鼠标选择文字被删除的解决办法

    copy from :http://blog.csdn.net/shadow066/article/details/50628019 在终端中输入命令:ibus-setup 将 “在应用程序窗口中启用 ...

  7. Idea工具常用技巧总结

    转自:https://www.jianshu.com/p/131c2deb3ecf Idea常用技巧总结 1.无处不在的跳转 注:这里的快捷键是自己定义的,并非大家的都一样,可以通过findActio ...

  8. UVA 1650 Number String

    https://vjudge.net/problem/UVA-1650 题意:D表示比前一个数打,I表示比前一个数小,?表示不确定 给出一个长为n由D I?组成的字符串,问满足字符串大小要求的n+1的 ...

  9. ZOJ 3774 二次剩余

    LINK 题意:简单粗暴,求菲波那契数列每个数的m次的前n项和模1e9+7 思路:斐波那契通项式, 注意到有很多根号5,求二次剩余为5模1e9+7的解,显然我们可以直接找一个(383008016),然 ...

  10. CSS浏览器兼容问题集-第三部分

    FF与IE 1. Div居中问题 div设置 margin-left, margin-right 为 auto 时已经居中,IE 不行,IE需要设定body居中,首先在父级元素定义text-algin ...