public class Result<T> { private Integer code; private String msg; private T data; public Result() { super(); } public Result(Integer code, String msg, T data) { this.code = code; this.msg = msg; this.data = data; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } @Override public String toString() { return "Result{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}'; } }
public enum ResultEnum { // here is their definition, to facilitate interaction with distal UNKNOWN_ERROR (-1, "Unknown error" ), SUCCESS ( 200 is, "success" ), SERVER_INTERNAL_ERROR ( 500, "Internal Server Error" ), RESOURCE_NOT_FOUND ( 404, "resource not found" ), PARAMETER_NOT_VALID ( 400, "invalid parameters" ), ; Private Integer code; Private String MSG; ResultEnum (code Integer, String MSG) { the this .code = code; the this a .msg = msg; } public Integer getCode() { return code; } public String getMsg() { return msg; } }
public class ResultUtil { / ** successful and the band data * * / public static the Result Success (Object Object) { the Result Result = new new the Result (); result.setCode (ResultEnum.SUCCESS.getCode ()); result.setMsg (ResultEnum. SUCCESS.getMsg ()); result.setData (Object); return Result; } / ** successful but no data * * / public static the Result success () { return success ( null ); } / ** failed * * / public static Result error(Integer code,String msg){ Result result = new Result(); result.setCode(code); result.setMsg(msg); return result; } }
//测试 @RequestMapping(value = "/list", method = RequestMethod.POST) public Result findCategoryByPid(@RequestBody Map map){ map.get("parentId"); List<Category> list = categoryService.findCategoryByPid(map); if (CollectionUtils.isEmpty(list)){ return ResultUtil.error(404,"资源未找到到"); } return ResultUtil.success(list); }