2-统一返回类DTO对象

这个博客介绍了Java中用于构建通用响应结果的`ResponseResult`类,包括成功和错误的处理方式。类包含了状态码、错误信息、响应消息和数据字段。还展示了如何根据枚举类型`AppHttpCodeEnum`生成不同类型的响应,并提供了`PageResponseResult`用于分页查询的响应。此外,`PageRequestDto`用于检查分页请求参数。
摘要由CSDN通过智能技术生成

通用结果返回类 (model模块)

image-20220727220540318

DTO

ResponseResult

package com.zs.model.common.dtos;


import com.alibaba.fastjson.JSON;
import com.zs.model.common.enums.AppHttpCodeEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通用的结果返回类
 * @param <T>
 */
@Data
public class ResponseResult<T> implements Serializable {

    /**
     * 访问地址
     */
    private String host;

    /**
     * 响应码
     */
    private Integer code;

    /**
     * 异常信息
     */
    private String errorMessage;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    public ResponseResult() {
        this.code = 200;
    }

    public ResponseResult(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public ResponseResult(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
    }

    public ResponseResult(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponseResult(Integer code, String errorMessage, String message, T data) {
        this.code = code;
        this.errorMessage = errorMessage;
        this.message = message;
        this.data = data;
    }

    public ResponseResult(String host, Integer code, String errorMessage, String message, T data) {
        this.host = host;
        this.code = code;
        this.errorMessage = errorMessage;
        this.message = message;
        this.data = data;
    }



    //返回ok信息 code + msg
    public static ResponseResult okResult(int code, String msg) {
        ResponseResult result = new ResponseResult();
        return result.ok(code, null, msg);
    }
    public ResponseResult<?> ok(Integer code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.message = msg;
        return this;
    }
    public ResponseResult<?> ok(Integer code, T data) {
        this.code = code;
        this.data = data;
        return this;
    }
    public ResponseResult<?> ok(T data) {
        this.data = data;
        return this;
    }
    //code + msg
    public static ResponseResult okResult(AppHttpCodeEnum enums){
        return setOkAppHttpCodeEnum(enums,enums.getMessage());
    }
    private static ResponseResult setOkAppHttpCodeEnum(AppHttpCodeEnum enums, String message){
        return okResult(enums.getCode(),message);
    }
    //200 + data
    public static ResponseResult okResult(Object data) {
        ResponseResult result = setOkAppHttpCodeEnum(AppHttpCodeEnum.SUCCESS, AppHttpCodeEnum.SUCCESS.getMessage());
        if(data!=null) {
            result.setData(data);
        }
        return result;
    }
    //enums的code + 自定义信息
    public static ResponseResult okResult(AppHttpCodeEnum enums, String errorMessage){
        return setOkAppHttpCodeEnum(enums,errorMessage);
    }


    //返回err信息 code + errmsg
    public static ResponseResult errorResult(int code, String errMsg) {
        ResponseResult result = new ResponseResult();
        return result.error(code, errMsg);
    }
    public ResponseResult<?> error(Integer code, String msg) {
        this.code = code;
        this.errorMessage = msg;
        return this;
    }
    //code + errMsg
    public static ResponseResult errorResult(AppHttpCodeEnum enums){
        return setErrAppHttpCodeEnum(enums,enums.getMessage());
    }
    private static ResponseResult setErrAppHttpCodeEnum(AppHttpCodeEnum enums, String message){
        return errorResult(enums.getCode(),message);
    }
    //enums的code + 自定义信息
    public static ResponseResult errorResult(AppHttpCodeEnum enums, String errorMessage){
        return setErrAppHttpCodeEnum(enums,errorMessage);
    }
    public static ResponseResult errorResultMessage(AppHttpCodeEnum enums, String errorMessage){
        return setErrAppHttpCodeEnum(enums,errorMessage);
    }
    private static ResponseResult setErrMsgAppHttpCodeEnum(AppHttpCodeEnum enums, String message){
        return errorResult(enums.getCode(),message);
    }
    public static ResponseResult errorResult(int code,String msg,String errMsg) {
        ResponseResult result = new ResponseResult();
        return result.error(code, msg,errMsg);
    }
    public ResponseResult<?> error(Integer code,String msg, String errMsg) {
        this.code = code;
        this.message = msg;
        this.errorMessage = errMsg;
        return this;
    }







    public static void main(String[] args) {
        //前置
        /*AppHttpCodeEnum success = AppHttpCodeEnum.SUCCESS;
        System.out.println(success.getCode());
        System.out.println(success.getErrorMessage());*/

        //查询一个对象
        /*Map map = new HashMap();
        map.put("name","zhangsan");
        map.put("age",18);
        ResponseResult result = ResponseResult.okResult(map);
        System.out.println(JSON.toJSONString(result));*/


        //新增,修改,删除  在项目中统一返回成功即可
       /* ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        System.out.println(JSON.toJSONString(result));*/


        //根据不用的业务返回不同的提示信息  比如:当前操作需要登录、参数错误
        /*ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        System.out.println(JSON.toJSONString(result));*/

        //查询分页信息
        PageResponseResult responseResult = new PageResponseResult(1,5,50);
        List list = new ArrayList();
        list.add("itcast");
        list.add("itheima");
        responseResult.setData(list);
        System.out.println(JSON.toJSONString(responseResult));
    }
}

PageResponseResult

@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageResponseResult extends ResponseResult implements Serializable {
    private Integer currentPage;
    private Integer size;
    private Integer total;
}

PageRequestDto

@Data
@Slf4j
public class PageRequestDto {

    protected Integer size;
    protected Integer page;

    public void checkParam() {
        if (this.page == null || this.page < 0) {
            setPage(1);
        }
        if (this.size == null || this.size < 0 || this.size > 100) {
            setSize(10);
        }
    }
}

Enum

AppHttpCodeEnum

package com.zs.model.common.enums;

public enum AppHttpCodeEnum {

    // 成功段0
    SUCCESS(0,"操作成功"),
    // 登录段1~50
    NEED_LOGIN(1,"需要登录后操作"),
    LOGIN_PASSWORD_ERROR(2,"密码错误"),
    // TOKEN50~100
    TOKEN_INVALID(50,"无效的TOKEN"),
    TOKEN_EXPIRE(51,"TOKEN已过期"),
    TOKEN_REQUIRE(52,"TOKEN是必须的"),
    // SIGN验签 100~120
    SIGN_INVALID(100,"无效的SIGN"),
    SIG_TIMEOUT(101,"SIGN已过期"),
    // 参数错误 500~1000
    PARAM_REQUIRE(500,"缺少参数"),
    PARAM_INVALID(501,"无效参数"),
    PARAM_IMAGE_FORMAT_ERROR(502,"图片格式有误"),
    SERVER_ERROR(503,"服务器内部错误"),
    // 数据错误 1000~2000
    DATA_EXIST(1000,"数据已经存在"),
    AP_USER_DATA_NOT_EXIST(1001,"ApUser数据不存在"),
    DATA_NOT_EXIST(1002,"数据不存在"),
    // 数据错误 3000~3500
    NO_OPERATOR_AUTH(3000,"无权限操作"),
    NEED_ADMIND(3001,"需要管理员权限");

    int code;
    String message;

    AppHttpCodeEnum(int code, String errorMessage){
        this.code = code;
        this.message = errorMessage;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠闲的线程池

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值