Java工具类-基础工具代码(Msg、PageResult、Response、常量、枚举)(可复制)

Msg(一)

package com.geespace.microservices.data.computing.model.server.response;

import java.util.HashMap;
import java.util.Map;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * Msg
 * 
 * @author:
 * @date: 2023-12-18
 */
@AllArgsConstructor
@Getter
public enum Msg {

    /**
     * 请求类型错误
     */
    SUCCESS(200, "success"), FAILED(20001, "Operation failed"),
    EXCUTE_ERROR(16001, "Service invocation exception, please try again later"),
    UNKNOW_ERROR(16002, "unknown error"),
    PARAM_ERROR(16003, "request param error"),
    DATA_SAVE_FAIL(16004, "data save fail"),
    ID_IS_NOT_EXIST(16005, "id isn't exist"),
    QUERY_RESULT_IS_EMPTY(16006, "request result is null"),
    NAME_IS_EXIST(16007, "name already exist"),
    ID_IS_NOT_EXIST_DATA(16008, "There is no data for the current ID"),
    DATASOURCE_NOT_EXIST(16009, "datasource not exist"),
    JDBC_EXCEPTION(16010, "jdbc connection or search exception"),
    TASK_HAS_BEEN_STOPPED(16011, "task has been stopped");

    private int code;
    private String msg;

    private static final Map<Integer, Msg> MAP = new HashMap<>();

    static {
        for (Msg msgEnum : Msg.values()) {
            MAP.put(msgEnum.code, msgEnum);
        }
    }

    /**
     * 通过code获取枚举
     *
     * @param code
     *            code
     * @return 枚举
     */
    public static Msg getByCode(int code) {
        return MAP.get(code);
    }
}

Msg(二) 简易版



public class HttpStatus {
    public static final Integer OK = 200;
    public static final Integer SERVER_ERROR = 500;
    public static final Integer NO_FOUND = 404;
    public static final Integer FORBIDDEN = 403;

    public HttpStatus() {
    }
}

PageResult(一)

package com.geespace.microservices.data.computing.model.server.response;

import java.io.Serializable;
import java.util.List;

import lombok.Data;

/**
 *
 * @param <T>
 *            Construct return entity
 * @since Oracle JDK1.8
 **/
@Data
public class PageResult<T> implements Serializable {
    private int pageNum;
    private int pageSize;
    private long totalCount;
    private long totalPage;
    private List<T> list;
}

PageResult(二) 简易版


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * 分页对象封装
 *
 * @author dj
 * @date 2023/12/07
 */
@Data
@Builder // builder模式
@NoArgsConstructor // 无参构造
@AllArgsConstructor // 全参构造
@ApiModel(description = "分页返回封装")
public class PageResult<T> implements Serializable {

    /**
     * 当前页
     */
    @ApiModelProperty(value = "当前页", example = "1")
    private Long pageNum = 1L;

    /**
     * 总页数
     */
    @ApiModelProperty(value = "页数", example = "10")
    private Long pageSize = 1L;

    /**
     * 数据
     */
    @ApiModelProperty(value = "操作对象返回数据", example = "list<T>")
    private List<T> records;

    @ApiModelProperty(value = "total", example = "1")
    private Long total = 1L;


}

Response(一)

package com.geespace.microservices.data.computing.model.server.response;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * merge fromn iot
 * 
 * @param <T>
 * @Version 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public final class Response<T> implements Serializable {

    private int code;
    private String msg;
    private T info;

    private Response(Msg msg) {
        this(msg, null);
    }

    private Response(Msg msg, T info) {
        this.code = msg.getCode();
        this.msg = msg.getMsg();
        this.info = info;
    }

    private Response(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * Return to success
     *
     * @param <T>
     *            Return info
     * @return <T> Success
     */
    public static <T> Response<T> success() {
        return success(null);
    }

    /**
     * Return to success
     *
     * @param t
     *            Return info
     * @param <T>
     *            Return generic
     * @return <T> Return entity
     */
    public static <T> Response<T> success(T t) {
        return new Response<>(Msg.SUCCESS, t);
    }

    /**
     * Return failed
     *
     * @param msg
     *            result enumeration
     * @param <T>
     *            Return info
     * @return <T> Return entity
     */
    public static <T> Response<T> error(Msg msg) {
        return new Response<>(msg);
    }

    /**
     * Return failed
     *
     * @param code
     *            Error code
     * @param msg
     *            Error message
     * @param <T>
     *            Return info
     * @return <T> Return entity
     */
    public static <T> Response<T> error(int code, String msg) {
        return new Response<>(code, msg);
    }

    /**
     *
     * @author luke liu
     * @param <T>
     *            t
     * @param msg
     *            error msg
     * @return error result
     **/
    public static <T> Response<T> error(String msg) {
        return new Response(msg);
    }

    /**
     * Determine whether response is successful in returning
     *
     * @return Success
     */
    public boolean responseSuccess() {
        if (this.code == Msg.SUCCESS.getCode()) {
            return true;
        }
        return false;
    }

    private Response(String msg) {
        this.code = Msg.FAILED.getCode();
        this.msg = msg;
        this.info = null;
    }
}

Response(二) 简易版


import com.****.PageResult;
import lombok.Data;

/**
 * @author dj
 * @date 2023/12/05
 */
@Data
public class ResponseMessage<T> {
    private static final String SUCCESS_MSG = "请求成功";
    private static final String ERROR_MSG = "请求失败";
    private int code;
    private Object data;
    private long total;
    private String message;
    private PageResult<T> pageResult;

    public ResponseMessage() {
    }

    public static <T>ResponseMessage<T> success() {
        return responseMessage(HttpStatus.OK, null, "请求成功", -1L);
    }

    public static <T>ResponseMessage<T> success(Object data) {
        return responseMessage(HttpStatus.OK, data, "请求成功", -1L);
    }

    public static <T>ResponseMessage<T> success(String msg) {
        return responseMessage(HttpStatus.OK, null, msg, -1L);
    }

    public static <T>ResponseMessage<T> success(Object data, String msg) {
        return responseMessage(HttpStatus.OK, data, msg, -1L);
    }
    public static <T>ResponseMessage<T> success(Object data, String msg, Long total) {
        return responseMessage(HttpStatus.OK, data, msg, total);
    }

    public static <T>ResponseMessage<T> responseMessage(int code, Object data, String message, long total) {
        ResponseMessage response = new ResponseMessage();
        response.setData(data);
        response.setCode(code);
        response.setMessage(message);
        response.setTotal(total);
        return response;
    }

    public static <T>ResponseMessage<T> responseMessage(int code, PageResult<T> pageResult, String message, long total) {
        ResponseMessage response = new ResponseMessage();
        response.setPageResult(pageResult);
        response.setCode(code);
        response.setMessage(message);
        response.setTotal(total);
        return response;
    }

    public static <T>ResponseMessage<T> successPage(Object data, long total) {
        return responseMessage(HttpStatus.OK, data, "请求成功", total);
    }

    public static <T>ResponseMessage<T> error() {
        return responseMessage(HttpStatus.SERVER_ERROR, null, "请求失败", -1L);
    }

    public static <T>ResponseMessage<T> error(String msg) {
        return responseMessage(HttpStatus.SERVER_ERROR, null, msg, -1L);
    }

    public static <T>ResponseMessage<T> error(Integer total, String msg) {
        return responseMessage(HttpStatus.SERVER_ERROR, null, msg, total);
    }




}

BusinessDomainEnum(枚举)

package com.geespace.microservices.directory.assets.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 业务域
 * @Author: dj
 * @Date: 2023/12/18
 * @Version 1.0
 */
@AllArgsConstructor
@Getter
public enum BusinessDomainEnum {
    /**
     * 类型1
     */
    ONE(1, "one"),
    /**
     * 类型2
     */
    TWO(2, "two");
    private int category;
    private String value;

    /**
     * @author      dj
     * @param       category category
     * @return      ture or false
     **/
    public static String transfer(int category) {
        BusinessDomainEnum[] values = BusinessDomainEnum.values();
        for (BusinessDomainEnum subEnum : values) {
            if (category == subEnum.getCategory()) {
                return subEnum.getValue();
            }
        }
        return "error";
    }
}

EsDocumentConstants(常量) 

package com.geespace.microservices.directory.assets.constants;

/**
 * 资产目录功能相关符号常量
 * @author: dj
 * @date: 2023/12/18
 */
public final class EsDocumentConstants {
    private EsDocumentConstants(){}

    public static final String FILE_NAME = "file_name";
    public static final String FILE_TYPE = "file_type";
    public static final String DATABASE_NAME = "database_name";
    public static final String TABLE_NAME = "table_name";
    public static final String INCLUDE_FIELDS = "include_fields";
    public static final String BUSINESS_DOMAIN = "business_domain";
    public static final String STORE_TYPE = "store_type";
    public static final String UPDATE_TIME = "update_time";
    public static final String WHETHER_ONLINE = "whether_online";
    public static final String FOREIGN_ID = "foreign_id";
}

StatusConstant(常量)超简易版需要再补充

/**
 * 状态
 * @author dj
 * @date 2023/12/06
 */
public class StatusConstant {

    public static final String SUCCESS = "成功";
    public static final String FAILURE = "失败";
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值