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. 金牌架构师:我们是这样设计APP数据统计产品的

    前言:近期,智能大数据服务商“个推”推出了应用统计产品“个数”,今天我们就和大家来谈一谈个数实时统计与AI数据智能平台整合架构设计. 很多人可能好奇,拥有数百亿SDK的个推,专注消息推送服务多年,现在 ...

  2. 运行python时提示:ImportError: No module named plyvel ,ImportError No module named irc 解决过程:

    (当前python版本:2.7) 1.在git下载electrum-server: cd / git clone https://github.com/spesmilo/electrum-server ...

  3. virt-manager管理器新建虚拟机时出错:unsupported format character

    启动管理器出错:unsupported format character ‘��0xffffffef) at index 30 经验证,解决办法如下: 1.获取virt-manager的rpm包,并强 ...

  4. bootstrap datetimepicker的参数解释

    使用bootstrap datetimepicker(日期时间选择器)的过程中,发现中文参数说明和英文参数说明严重不符,所以结合自己使用的情况和英文参数说明,做了如下翻译. $(".form ...

  5. 题解 UVA1184 【Air Raid】

    有向无环图(DAG)的最小路径覆盖的模板题. 定义:在一个有向图中,找出最少的路径,使得这些路径经过了所有的点. 由题意可得这是一个有向图,而路径不能相交,于是我们知道这是无向图的不相交最小路径覆盖问 ...

  6. codevs 1491 取物品

    1491 取物品 http://codevs.cn/problem/1491/  时间限制: 1 s  空间限制: 128000 KB     题目描述 Description 现在有n个物品(有可能 ...

  7. 分块基础练习 UESTC 1324

    http://acm.uestc.edu.cn/#/problem/show/1324 思路:基础分块,这个是一个特别简单的分块,就当做是一个练习了.然后这题也是很简单的单点线段树更新. //看看会不 ...

  8. Java--图片浏览器

    功能:启动后选择打开文件,可以打开图片进行浏览. v 1.0 :支持上一张 下一张功能.(欠缺,窗口大小未随着图片大小而改变) import java.awt.BorderLayout; import ...

  9. JAVA多线程提高九:Semaphere同步工具

    java 中Semaphere可类比操作系统信号量,硬件资源如IO.内存.磁盘等都是有固定量的,多个程序需要竞争这些资源,没有资源就需要被挂起. 一.类和方法摘要 构造函数: public Semap ...

  10. windows 安装elasticsearch-head插件

    es5以上版本安装head需要安装node和grunt(之前的直接用plugin命令即可安装) (一)从地址:https://nodejs.org/en/download/ 下载相应系统的msi,双击 ...