zoukankan      html  css  js  c++  java
  • 统一接口返回值的定义

    /**
    * 返回给前台页面的值的封装类
    * @author x_luwl
    *
    */
    public class ResponseMessage<T> {


    private int code;

    private String msg;

    private T result;

    public int getCode() {
    return code;
    }

    public void setCode(int code) {
    this.code = code;
    }

    public String getMsg() {
    return msg;
    }

    public void setMsg(String msg) {
    this.msg = msg;
    }

    public T getResult() {
    return result;
    }

    public void setResult(T result) {
    this.result = result;
    }

    }

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    public enum ResultEnum {

    UNKNOWN_ERROR(99999, "未知错误"),
    //1开头为参数
    LOCAL_ERROR(10001,"路径访问错误"),
    PARAM_ERROR(10002,"路径参数错误"),
    PARAM_JIEXI_ERROR(10003,"参数解析错误"),
    PARAM_YANZHENG_ERROR(10004,"参数验证错误"),
    PARAM_BANGDING_ERROE(10005,"参数绑定失败"),

    HTTP_SENDREQUEST_ERROR(20001,"不支持当前请求方法"),
    THE_MEDIA_TYPE_ERROE(20002,"不支持当前媒体类型"),

    THE_CODE_DO_ERROE(30001,"业务逻辑异常"),
    //后台报错返回的给前台的消息台
    EXCEPTION_ERROR(21001,"系统繁忙"),
    SYSTEM_ERROR(21002,"系统错误"),
    SQL_ERROR(21003,"数据库错误");

    private Integer code;

    private String msg;

    private ResultEnum(Integer code, String msg) {
    this.code = code;
    this.msg=msg;
    }

    public Integer getCode() {
    return code;
    }

    public String getMsg() {
    return msg;
    }
    }

     ----------------------------------------------------------------------------------------------------------------------------------------------------------

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;


    /**
    * 统一返回值工具
    * @author x_luwl
    *
    */
    public class ResultUtil{


    private static final Logger LOGGER = LoggerFactory.getLogger(ResultUtil.class);

    /**
    * 请求成功返回
    * @param object
    * @return
    */
    public static ResponseMessage success(Object object){
    ResponseMessage msg=new ResponseMessage();
    msg.setCode(200);
    msg.setMsg("请求成功");
    msg.setResult(object);
    LOGGER.info("请求数据成功,数据值为:"+object);
    return msg;
    }
    public static ResponseMessage success(){
    LOGGER.info("请求数据成功");
    return success(null);
    }

    public static ResponseMessage error(Integer code,String resultmsg){
    ResponseMessage msg=new ResponseMessage();
    msg.setCode(code);
    msg.setMsg(resultmsg);
    return msg;
    }


    }

    使用:

    Controller 返回值为ResponseMessage

    return ResultUtil.success(obj);

  • 相关阅读:
    CSS-calc 兼容写法
    一行代码轻松搞定各种IE兼容问题,IE6,IE7,IE8,IE9,IE10
    地图调起URI API(通过连接直接调用百度地图)
    获取字体图标(阿里图标网站)
    CSS-解决苹果点击高亮、安卓select灰色背景(select下拉框在IOS中背景变黑、出现阴影问题)
    移动端 input 输入框实现自带键盘“搜索“功能并修改X
    常用正则表达式—邮箱(Email)
    JS正则表达式验证是否为11位有效手机号码
    input,textarea在ios和Android上阴影和边框的处理方法(在移动端)
    three.map.control
  • 原文地址:https://www.cnblogs.com/Sora-L/p/9365053.html
Copyright © 2011-2022 走看看