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. codeforces 55D 数位dp

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  2. 使用jconsole工具来监控java运行情况

    参考:http://blog.163.com/lucas_nina/blog/static/185960149201493034258448/   经验证OK   jconsole是jdk自带的工具. ...

  3. OpenCV---图像加载与保存

    一:获取图像的信息 什么是图像: 结构化存储的数据信息 图像属性: -通道数目 -高与宽 -像素数据 -位图深度 import cv2 as cv def get_image_info(image): ...

  4. springboot+spring session+redis+nginx实现session共享和负载均衡

    环境 centos7. jdk1.8.nginx.redis.springboot 1.5.8.RELEASE session共享 添加spring session和redis依赖 <depen ...

  5. 小程序制作中 一个奇怪的bug

    事情是这样的:原一个购物车 合并本地数据和服务器 数据方法如下 ,正常测试没有问题,当每次重新登录,会调用到这个方法,就会莫名其妙的卡主,debug 发现 a1.length =77731508 导致 ...

  6. Vue 脱坑记

    问题汇总 Q:安装超时(install timeout) 方案有这么些: cnpm : 国内对npm的镜像版本 /* cnpm website: https://npm.taobao.org/ */ ...

  7. 蓝色的PC端后台管理界面设计模板——后台

    链接:http://pan.baidu.com/s/1o82hXX4 密码:x6le

  8. python批量替换文件名

    替换关键字 #-*-coding:utf-8-*- import os import re filepath = u'E:\\CMMI4\\07_测试文档' files = os.walk(filep ...

  9. Java 序列化工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Decoder; import sun.m ...

  10. 简约而不简单的Django

    本文面向:有python基础,刚接触web框架的初学者. 环境:windows7   python3.5.1  pycharm专业版  Django 1.10版 pip3 一.Django简介 百度百 ...