java 接口请求返回通用json

java

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 的全部内容, 来源链接: utcz.com/z/393521.html

回到顶部