在开发简单的后端功能时,应该做到响应和结果统一,方便交接及前后端联调:
class ResultInfo( val code: Int, val message:String, var data:Any?) { companion object { fun success(data:Any? = null, message: String = "success"): ResultInfo{ return ResultInfo(200, message, data) } fun error(data:Any? = null, message: String = "error", code: Int = 500): ResultInfo{ return ResultInfo(code, message, data) } fun tryit(op : ()-> Any?): ResultInfo{ return try { success(op.invoke()) }catch (e: Exception){ error(message = e.message ?: "") } } } }
返回结果只需通过对象调用tryit接口,把查出来的结果集传入即可(任何结果都行)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)