zoukankan      html  css  js  c++  java
  • springboot返回数据统一格式

    1.ReturnVo.java

    
    
    import cn.ichangyun.integration.core.entity.ServerEntity;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class ReturnVo extends HashMap<String, Object> {
    
    	private static final long serialVersionUID = 1L;
    	private static final String RESULT = "result";
    
    	public ReturnVo() {
    		put("code", 0);
    		put("msg", "success");
    	}
    
    	/**
    	 * 未知异常返回
    	 */
    	public static ReturnVo error() {
    		return error(500, "未知异常,请联系管理员");
    	}
    
    	/**
    	 * 未知异常返回
    	 */
    	public static ReturnVo sessionError() {
    		return error(401, "session失效");
    	}
    
    	/**
    	 * 返回异常信息
    	 */
    	public static ReturnVo error(String msg) {
    		return error(500, msg);
    	}
    
    	/**
    	 * 返回异常状态码,信息
    	 */
    	public static ReturnVo error(int code, String msg) {
    		ReturnVo returnVo = new ReturnVo();
    		returnVo.put("code", code);
    		returnVo.put("msg", msg);
    		return returnVo;
    	}
    
    	/**
    	 * 成功信息
    	 */
    	public static ReturnVo ok(String msg) {
    		ReturnVo returnVo = new ReturnVo();
    		returnVo.put("msg", msg);
    		return returnVo;
    	}
    
    	public static ReturnVo ok(Map<String, Object> map) {
    		ReturnVo returnVo = new ReturnVo();
    		returnVo.putAll(map);
    		return returnVo;
    	}
    
    	public static ReturnVo ok() {
    		return new ReturnVo();
    	}
    
    	@Override
    	public ReturnVo put(String key, Object value) {
    		super.put(key, value);
    		return this;
    	}
    	public ReturnVo serverResult(ServerEntity serverEntity) {
    		put(RESULT, serverEntity);
    		return this;
    	}
    }
    
    

    2.使用示例

    return ReturnVo.ok();
    return ReturnVo.error("错误信息");
    return ReturnVo.ok().put("data",data);
    
    
  • 相关阅读:
    SQLite out of order error备忘
    SQLITE_TOOBIG
    Android CursorWindow问题备忘
    SQLite3神奇的UNION、UNION ALL与LIMIT组合
    Android Database(SQLite)参数绑定问题初探
    Android SQLite 加入自定义函数
    修改替换/system/framework/framework.jar后重启手机为何没有效果?
    手动调用NDK编译HelloWorld
    第一篇
    程序题
  • 原文地址:https://www.cnblogs.com/xian-yu/p/13267359.html
Copyright © 2011-2022 走看看