程序员

Java 接口返回类

2016-11-10  本文已影响0人  碳头tantou

本文说明在业务系统中,如何设计返回值接口类,包括普通数据和分页数据

基类

提供基础字段,包括:success,message,code

代码

package com.tantou.maven.result;

import java.io.Serializable;

/**
 * Created by jiangweiyang on 16/11/10.
 */
public class ResultSupport implements Serializable {
    private static final long serialVersionUID = -2235152751651905167L;

    public boolean isSuccess() {
        return code == ResultCode.SuccessCode;
    }


    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getCode() {
        return code;
    }

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

    private String message;
    private int code;
}

普通数据

继承自ResultSupport,多了data字段,可以存储数组和普通对象

数据格式

数组

{
    message: null,
    code: 0,
    data: [],
    success: true
}

对象

{
    message: null,
    code: 0,
    data: {},
    success: true
}

代码

package com.tantou.maven.result;

/**
 * Created by jiangweiyang on 16/11/10.
 */
public class APIResult<T> extends ResultSupport {
    protected T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 接口调用失败,有错误字符串码和描述,有返回对象
     * @param code
     * @param message
     * @param data
     * @param <U>
     * @return
     */
    public static <U> APIResult<U> newFailResult(int code, String message, U data) {
        APIResult<U> apiResult = new APIResult<U>();
        apiResult.setCode(code);
        apiResult.setMessage(message);
        apiResult.setData(data);
        return apiResult;
    }

    /**
     * 接口调用失败,有错误字符串码和描述,没有返回对象
     * @param code
     * @param message
     * @param <U>
     * @return
     */
    public static <U> APIResult<U> newFailResult(int code, String message) {
        APIResult<U> apiResult = new APIResult<U>();
        apiResult.setCode(code);
        apiResult.setMessage(message);
        return apiResult;
    }


    public static <U> APIResult<U> newSuccessResult(U data){
        APIResult<U> apiResult = new APIResult<U>();
        apiResult.setData(data);
        return apiResult;
    }
}

分页数据

继承自ResultSupport,多了metadata属性,meta用于存放分页相关信息,也可以扩展成其他元数据信息,data用与存放数组信息

数据格式

{
    message: null,
    code: 0,
    data: [],
    meta: {
        pageNo: 1,
        pageSize: 2,
        totalCount: 20
    },
    success: true
}

代码

package com.tantou.maven.result;

import java.util.Collection;

/**
 * Created by jiangweiyang on 16/11/10.
 */
public class PageResult<T extends Collection> extends ResultSupport {

    protected T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    private Meta meta;

    public Meta getMeta() {
        return meta;
    }

    public void setMeta(Meta meta) {
        this.meta = meta;
    }

    public int getPageNo() {
        return meta.getPageNo();
    }

    public void setPageNo(int pageNo) {
        this.meta.setPageNo(pageNo);
    }

    public int getPageSize() {
        return this.meta.getPageSize();
    }

    public void setPageSize(int pageSize) {
        this.meta.setPageSize(pageSize);
    }

    public int getTotalCount() {
        return this.meta.getTotalCount();
    }

    public void setTotalCount(int totalCount) {
        this.meta.setTotalCount(totalCount);
    }

    public PageResult() {
        this.meta = new Meta();
    }

    public PageResult(int pageNo, int pageSize, int totalCount, T data) {
        this();
        this.setPageNo(pageNo);
        this.setPageSize(pageSize);
        this.setTotalCount(totalCount);
        this.setData(data);
    }

    /**
     * 接口调用失败,有错误字符串码和描述,有返回对象
     *
     * @param code
     * @param message
     * @param data
     * @param <U>
     * @return
     */
    public static <U extends Collection> PageResult<U> newFailResult(int code, String message, U data) {
        PageResult<U> pageResult = new PageResult<U>();
        pageResult.setCode(code);
        pageResult.setMessage(message);
        pageResult.setData(data);
        return pageResult;
    }

    /**
     * 接口调用失败,有错误字符串码和描述,没有返回对象
     *
     * @param code
     * @param message
     * @param <U>
     * @return
     */
    public static <U extends Collection> PageResult<U> newFailResult(int code, String message) {
        PageResult<U> pageResult = new PageResult<U>();
        pageResult.setCode(code);
        pageResult.setMessage(message);
        pageResult.setMeta(null);
        return pageResult;
    }

    public static <U extends Collection> PageResult<U> newSuccessResult(int pageNo, int pageSize, int totalCount, U data) {
        return new PageResult<U>(pageNo, pageSize, totalCount, data);
    }

    private class Meta {
        protected int pageNo = 1;
        protected int pageSize;
        protected int totalCount;

        public int getPageNo() {
            return pageNo;
        }

        public void setPageNo(int pageNo) {
            if (pageNo <= 0) {
                pageNo = 1;
            }
            this.pageNo = pageNo;
        }

        public int getPageSize() {
            return pageSize;
        }

        public void setPageSize(int pageSize) {
            if (pageSize < 0) {
                pageSize = 0;
            }
            this.pageSize = pageSize;
        }

        public int getTotalCount() {
            return totalCount;
        }

        public void setTotalCount(int totalCount) {
            this.totalCount = totalCount;
        }
    }
}

状态码

系统

public abstract class ResultCode {
    public static final int SuccessCode = 0;
}

业务

public static final Long DefaultAddressNotDeleteErrorCode = 10001L;//默认地址不能删除
public static final Long NotFindAddressErrorCode = 10002L;//找不到此收货地址
public static final Long NotFindUserErrorCode = 10003L;//找不到此用户
public static final Long NotMatchUserAddressErrorCode = 10004L;//用户与收货地址不匹配

应用

@RequestMapping(method = RequestMethod.GET, value = "/success")
public PageResult<List<User>> findPageResultSuccess(HttpServletRequest req) {
    List<User> users = new ArrayList<User>();
    for (int i = 0; i < 20; i++) {

        User tom = new User();
        tom.setUsername("tom" + i);
        users.add(tom);
    }
    PageResult<List<User>> result = PageResult.newSuccessResult(1, 2, users.size(), users);
    return result;
}

@RequestMapping(method = RequestMethod.GET, value = "/fail")
public PageResult<List<User>> findPageResultFail(HttpServletRequest req) {
    List<User> users = new ArrayList<User>();
    PageResult<List<User>> result = PageResult.newFailResult(1, "分页查询失败");
    return result;
}

@RequestMapping(method = RequestMethod.GET, value = "/success")
public APIResult<User> findAPIResultSuccess(HttpServletRequest req) {
    User user = new User();
    APIResult<User> result = APIResult.newSuccessResult(user);
    return result;
}

@RequestMapping(method = RequestMethod.GET, value = "/fail")
public APIResult<User> findAPIResultFail(HttpServletRequest req) {
    APIResult<User> result = APIResult.newFailResult(1, "没有找到用户");
    return result;
}
上一篇下一篇

猜你喜欢

热点阅读