当前位置 : 主页 > 编程语言 > java >

接口内容规范化

来源:互联网 收集:自由互联 发布时间:2021-06-28
emun 统一管理 返回的code 和msg public enum ResultEnums {SUCCESS(0,"成功"),NNKOWN_ERROR(1,"未知错误");private int code;private String msg;private ResultEnums(int code, String msg) {this.code = code;this.msg = msg;}public int ge
emun 统一管理 返回的code 和msg
public enum ResultEnums {
	SUCCESS(0,"成功"),
	NNKOWN_ERROR(1,"未知错误")
	;
	private int code;
	private String msg;

	private ResultEnums(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	public int getCode() {
		return code;
	}
	public String getMsg() {
		return msg;
	}

}
rusult 内容 自己写个返回的格式 ,并继承Map 增加扩展性
public class Rusult
 
   extends HashMap
  
    { private static final long serialVersionUID = 1L; private int code; private String msg; private T data; public Rusult(ResultEnums resultEnums, T data) { super(); code = resultEnums.getCode(); msg = resultEnums.getMsg(); this.data = data; } public JSONObject getJSON(){ JSONObject result = new JSONObject(); result.put("code", code); result.put("msg", msg); result.put("data", data); return result; } }
  
 
controller 使用案例
@RequestMapping(value="/test")
	private  void test(HttpServletRequest req , HttpServletResponse resp){
		log.info("test");
		Rusult resultlibai = null; 
		try{ 
			resultlibai = new  Rusult(ResultEnums.SUCCESS, 2226); resultlibai.put("dd", "1231"); log.info(resultlibai.get("dd")); // throw new Exception("dd"); }catch(Exception e){ resultlibai = new Rusult(ResultEnums.NNKOWN_ERROR, "libai2"); } Bean.outPrintWriter(resultlibai.getJSON(), resp); } 
上一篇:创建csv格式文件
下一篇:Box窗口测试
网友评论