• java 接口请求返回通用json


    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();
    			}
    		}
    	}
    }
    

      

      

  • 相关阅读:
    自省改过
    c语言学习教程1之预定义代码
    tqdm进度条管理
    React-i18next切换中英文
    React怎样实现点击其他地方隐藏弹出的菜单
    遍历对象再取值,获得新值
    Web 用户体验设计提升指南
    antd vue form
    echarts 内容显示值在图中显示,不显示交互效果,只是静态图
    element-ui中el-table根据浏览器的缩放自适应宽度
  • 原文地址:https://www.cnblogs.com/zuolun2017/p/6762196.html
Copyright © 2020-2023  润新知