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. nginx如何配置虚拟主机

    server { listen 80; #listen [::]:80 default_server ipv6only=on; server_name local.presion.caomall.ne ...

  2. crontab 自动执行脚本

    crontab -e ================>自动执行某脚本!!!!!!! 1001 ls 1002 cd /home/wwwroot/default/ 1003 ls 1004 cr ...

  3. Tensorflow实现学习率衰减

    Tensorflow实现学习率衰减 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Deeplearning AI Andrew Ng Tensorflow1.2 API 学习率衰减 ...

  4. 构造+分块思想 Codeforces Round #319 (Div. 1) C

    http://codeforces.com/contest/576/problem/C 题目大意: 给你一个曼哈顿距离的图,然后要求你找到一个链,链穿了所有的点 然后要求这链的长度<=25*10 ...

  5. awk是全局周期

    需要折行时需要用转译符,转译回车,回车是提交命令     \           如果你的命令中有单引号也可以  awk 支持C语言 awk '{name[$1]=name[$1]+$2} END{f ...

  6. 2017ACM暑期多校联合训练 - Team 6 1003 HDU 6098 Inversion (模拟)

    题目链接 Problem Description Give an array A, the index starts from 1. Now we want to know Bi=maxi∤jAj , ...

  7. 使用vscode实现git同步

    用了git最方便的就是项目同步管理,回到家打开vscode只需要点击一下pull就能全部同步过来.是不是很方便....毕竟之前我都是拿u盘拷贝回家或者存到云盘再下载下来..   我这里之前用的是国内的 ...

  8. JSON简介——(0)

    JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  9. 31 - gogs安装-git基础

    目录 1 Gogs安装 2 Git介绍 3 使用Github仓库 3.1 Git配置 3.2 远程仓库 4 Git基本使用 4.1 创建版本库 4.2 查看工作区状态 4.3 查看修改内容 4.4 查 ...

  10. python并发编程之asyncio协程(三)

    协程实现了在单线程下的并发,每个协程共享线程的几乎所有的资源,除了协程自己私有的上下文栈:协程的切换属于程序级别的切换,对于操作系统来说是无感知的,因此切换速度更快.开销更小.效率更高,在有多IO操作 ...