springmvc返回json参数封装
来源:互联网 发布:steam mac 编辑:程序博客网 时间:2024/06/01 08:45
现在大部分项目都用springmvc返回json参数来作为接口,但是又很难统一返回值格式和个性化定制返回值 尤其是作为APP接口 接口返回的状态码尤为重要 所以本人对springmvc返回json参数和返回值进行了一个简单封装 减少了重复的代码
其他返回值方法根据需要传参数即可
首先建立一个GeneralReturn.java类 集成了返回状态码 响应消息 是否成功 和初始化常见状态码消息
/** * Created by LaoWang on 2017/5/4. */public class GeneralReturn implements Serializable { private static Map<String,String>messageMap = Maps.newHashMap(); private String message; private boolean success = true; private int code; //初始化状态码与文字说明 static { messageMap.put("0", ""); messageMap.put("400", "HTTP 错误 400.0 - 访问被拒绝:错误的请求"); messageMap.put("401", "HTTP 错误 401.1 - 未经授权:访问由于凭据无效被拒绝"); messageMap.put("405", "用来访问本页面的 HTTP 谓词不被允许(方法不被允许)"); messageMap.put("406", "客户端浏览器不接受所请求页面的 MIME 类型"); messageMap.put("500", "HTTP 错误 500.0 - 服务器出错:内部服务器出错"); messageMap.put("1005", "[服务器]运行时异常"); messageMap.put("1006", "[服务器]空值异常"); messageMap.put("1007", "[服务器]数据类型转换异常"); messageMap.put("1008", "[服务器]IO异常"); messageMap.put("1009", "[服务器]未知方法异常"); messageMap.put("1010", "[服务器]数组越界异常"); messageMap.put("1011", "[服务器]网络异常"); } public GeneralReturn() { } public static GeneralReturn success() { GeneralReturn generalReturn = new GeneralReturn(); generalReturn.setSuccess(true); return generalReturn; } public static GeneralReturn success(String message) { GeneralReturn generalReturn = new GeneralReturn(); generalReturn.setSuccess(true); generalReturn.setMessage(message); return generalReturn; } public static GeneralReturn success(String message,int code) { GeneralReturn generalReturn = new GeneralReturn(); generalReturn.setSuccess(true); generalReturn.setCode(code); generalReturn.setMessage(message); return generalReturn; } public static GeneralReturn error(int code) { GeneralReturn generalReturn = new GeneralReturn(); generalReturn.setSuccess(true); generalReturn.setCode(code); generalReturn.setMessage(messageMap.get(String.valueOf(code))); return generalReturn; } public static GeneralReturn fail(String message) { GeneralReturn generalReturn = new GeneralReturn(); generalReturn.setSuccess(false); generalReturn.setCode(-1); generalReturn.setMessage(message); return generalReturn; } public static GeneralReturn fail(String message, int code) { GeneralReturn generalReturn = new GeneralReturn(); generalReturn.setSuccess(false); generalReturn.setCode(code); generalReturn.setMessage(message); return generalReturn; } public GeneralReturn put(String key, Object value) { this.data.put(key, value); return this; } public GeneralReturn putAll(Map<String, ?> map) { Iterator i$ = map.keySet().iterator(); while(i$.hasNext()) { String key = (String)i$.next(); Object value = map.get(key); if(value != null) { this.put(key, value); } } return this; } public boolean isEmpty() { return this.data == null || this.data.isEmpty(); } //省略get set 方法}
使用方法
@ResponseBody @RequestMapping(value = { "/workQueueTest.json" }, method = RequestMethod.POST) public GeneralReturn workQueueDemo(@RequestParam Integer forNum,HttpServletRequest request)throws Exception{ logger.info("WorkQueue队列实现演示"); if(forNum>=20){ return Response.fail("普通演示不允许超过20!"); } //这里只是为了说明在队列中的变量都要设置为私有的 final String key = "WorkQueue-Test"; long startTime=System.currentTimeMillis(); //获取开始时间 for(int i = 0; i<=forNum; i++){ workQueue.execute(new Runnable() { @Override public void run() { //具体队列需要执行的模块代码 logger.info("执行队列-----"+key); } }); } long endTime=System.currentTimeMillis(); //获取结束时间 return GeneralReturn.success((endTime-startTime)+" ms -队列执行时间"); }
其他返回值方法根据需要传参数即可
阅读全文
0 0
- springmvc返回json参数封装
- springMVC传对象参数、返回JSON格式数据
- SpringMVC返回json
- SpringMVC返回JSON格式
- springmvc返回json乱码
- springmvc 返回json配置
- Springmvc返回json
- springmvc 返回 json数据
- springmvc返回json数据
- springMVC 返回json值
- 配置springmvc返回json
- springMvc配置json返回
- SpringMVC返回json数据
- SpringMVC返回JSON
- SpringMVC返回JSON数据
- springmvc返回json
- springmvc 返回json
- springmvc返回json格式
- Scala--循环
- C# 几十万级数据导出Excel,及Excel各种操作
- jsoup用到的方法,大部分是网上查,官网看到的
- 前端视角漫谈百度ueditor编辑器前后端分离配置
- 【greenplum】greenplum 如何删除临时schema(孤立schema)
- springmvc返回json参数封装
- Java内存溢出(OOM)异常完全指南【转】
- druid源码解读
- SpringMVC(二)配置文件
- 美团云深度学习平台-快速开始
- Win下必备神器之Cmder
- 查看被占用的端口号,并杀死它
- RecyclerView学习
- 对一个字符串进行...(未完待续)