springmvc返回json参数封装

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014174854/article/details/78550638


现在大部分项目都用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 -队列执行时间");
    }

其他返回值方法根据需要传参数即可 

猜你喜欢

转载自blog.csdn.net/u014174854/article/details/78550638