java往前端ajax反数据实体类

前言

最近在springMVC框架中用到一个实体类,用来返回ajax请求,这里分享一下这个类。

具体用法

1.实体类

import java.io.Serializable;
/**
 1. 响应类
 */
public class ResponseVO<T> implements Serializable{

    private static final long serialVersionUID = -5007654155748473782L;

    /**
     * 状态码
     */
    private String code;
    /**
     * 消息(错误信息)
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    public String getCode() {
        return code;
    }
    public ResponseVO<T> setCode(String code) {
        this.code = code;
        return this;
    }
    public String getMsg() {
        return msg;
    }
    public ResponseVO<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }
    public T getData() {
        return data;
    }
    public ResponseVO<T> setData(T data) {
        this.data = data;
        return this;
    }

    /**
     * 状态码定义
     */
    public interface ResponseCode{

        /**
         * 成功
         */
        public static final String SUCCESS = "0";
        /**
         * 业务报错
         */
        public static final String ERROR = "1";
        /**
         * 程序异常
         */
        public static final String EXCEPTION = "-1";

    }

    /**
     * 默认创建方法
     * @return
     */
    public static <T> ResponseVO<T> build(){
        ResponseVO<T> responseVO = new ResponseVO<T>();
        return responseVO.setCode(ResponseCode.SUCCESS);
    }

    /**
     * 默认抛出异常
     * @return
     */
    public static <T> ResponseVO<T> exception(){
        ResponseVO<T> responseVO = new ResponseVO<T>();
        return responseVO.setCode(ResponseCode.EXCEPTION).setMsg("服务器发生异常,请联系管理员!");
    }

    /**
     * 默认错误
     * @return
     */
    public static <T> ResponseVO<T> error(){
        ResponseVO<T> responseVO = new ResponseVO<T>();
        return responseVO.setCode(ResponseCode.ERROR);
    }
}

2.后端用法

@ResponseBody
@RequestMapping(value = "getNames")
public ResponseVO<List<String>> getNames(String id) {
    List<String> names = new ArrayList<String>();
    try {
        names.add("zhangsan");
        names.add("lisi");
        return ResponseVO.<List<String>>build().setData(names);
    } catch (Exception e) {
        return ResponseVO.<List<String>>exception();
    }
}
@ResponseBody
@RequestMapping(value = "getNames")
public ResponseVO<Student> getNames(String id) {
    Student student = new Student();
    try {
        student.setName("zhangsan");
        student.setAge(21);
        return ResponseVO.<Student>build().setData(student);
    } catch (Exception e) {
        return ResponseVO.<List<String>>exception();
    }
}

以上列举2中数据类型

3.前端ajax用法

$.ajax({
    type: "POST",
    url: ctx + '/.../getNames',
    data: {id:id},
    dataType: "json",
    success: function(dataMap){
        if(dataMap.code == '0'){
            $.each(dataMap.data,function(k,v){
                ...
            });
        }else{
            layer.msg(dataMap.msg,{icon:7});
        }
    }
});

后记

这个类主要是用到了泛型类和泛型方法,使得它能够通用各种数据类型。

阅读更多

没有更多推荐了,返回首页