美文网首页
springboot中可能使用的工具类

springboot中可能使用的工具类

作者: 盗生一 | 来源:发表于2020-08-18 22:36 被阅读0次
  • @NotNull,@NotBlank和 @NotEmpty使用时 pojo验证注解类
package cn.hbeu.utils;
import java.util.ArrayList;
import java.util.List;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

/**
 * 校验pojo验证注解类
 */
public class Validation {
    
    public static List<?> getValidation(BindingResult result) {
        List<String> resultList = new ArrayList<>();
        if(result.hasFieldErrors()){
            List<FieldError> errorList = result.getFieldErrors();
            errorList.forEach(item -> resultList.add(item.getField()+" "+item.getDefaultMessage()));
        }
        return resultList;
    }
}
  • 常量 状态码 消息类
package cn.hbeu.common;

public class Constant {

    public interface BaseCode {
        int getStatusCode();

        String getStatusValue();
    }


    /**
     * 订单状态及状态对应的消息描述
     */
    public enum OrderStatus implements BaseCode {
        SUCCESS(200, "添加成功"),
        FAIL(100,"添加失败"),
        PAY_SUCCESS(1,"已付款"),
        PAY_WIAT(2,"未付款"),
        DEL_FAIL(100,"添加失败"),
        UPDATE_FAIL(100,"添加失败"),
        ;

        /**
         * 订单状态码
         */
        private int statusCode;

        /**
         * 状态码对应的值
         */
        private String statusValue;

        OrderStatus(int statusCode, String statusValue) {
            this.statusCode = statusCode;
            this.statusValue = statusValue;
        }

        public int getStatusCode() {
            return statusCode;
        }

        @Override
        public String getStatusValue() {
            return statusValue;
        }

    }

    /**
     * 商品
     */
    public enum GOODS implements BaseCode{
        SUCCESS(200, "添加成功"),
        FAIL(100,"添加失败"),
        EXIST(202,"商品已经存在"),
        DEL_FAIL(100,"删除失败"),
        ;

        /**
         * 订单状态码
         */
        private int statusCode;

        /**
         * 状态码对应的值
         */
        private String statusValue;

        GOODS(int statusCode, String statusValue) {
            this.statusCode = statusCode;
            this.statusValue = statusValue;
        }

        public int getStatusCode() {
            return statusCode;
        }

        public String getStatusValue() {
            return statusValue;
        }
    }

    /**
     * 商品
     */
    public enum PROVIDER implements BaseCode{
        SUCCESS(200, "添加成功"),
        FAIL(100,"添加失败"),
        EXIST(202,"供应商名称或编号已经存在"),
        DEL_FAIL(100,"删除失败"),
        ;

        /**
         * 订单状态码
         */
        private int statusCode;

        /**
         * 状态码对应的值
         */
        private String statusValue;

        PROVIDER(int statusCode, String statusValue) {
            this.statusCode = statusCode;
            this.statusValue = statusValue;
        }

        public int getStatusCode() {
            return statusCode;
        }

        public String getStatusValue() {
            return statusValue;
        }
    }
    /**
     * 数据状态组合
     * 1:正常
     * 2:删除
     */
    public interface IsDel {
        /**
         * 正常
         */
        int NORMAL = 1;
        /**
         * 删除(下架/)
         */
        int DEL = 2;
    }

}

-layUI table 返回包装类

package cn.hbeu.common;

import java.util.List;

public class LayUITableResult {

//      "code": 0,
//              "msg": "",
//              "count": 1000,
//              "data": [{}, {}]


    private Integer code;
    private String msg;
    private Long count;
    private List<?> data;

    public LayUITableResult(Integer code, String msg, Long count, List<?> data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public List<?> getData() {
        return data;
    }

    public void setData(List<?> data) {
        this.data = data;
    }
}

  • easyui datagraid返回值包装类
package cn.hbeu.common;

import java.util.List;

public class EasyUIDataGridResult {
    /**
     * 查询数据总条数
     */
    private Integer total;
    /**
     * 查询的所有数据
     */
    private List<?> rows;


    public EasyUIDataGridResult(Integer total, List<?> rows) {
        this.total = total;
        this.rows = rows;
    }
    public EasyUIDataGridResult(long total, List<?> rows) {
        this.total = (int)total;
        this.rows = rows;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public List<?> getRows() {
        return rows;
    }

    public void setRows(List<?> rows) {
        this.rows = rows;
    }
}

  • 高响应对象 json
package cn.hbeu.common;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

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

/**
 * 高复用响应对象
 */
public class ZResult implements Serializable {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    private Object data;

    public static ZResult build(Integer status, String msg, Object data) {
        return new ZResult(status, msg, data);
    }

    public static ZResult ok(Object data) {
        return new ZResult(data);
    }

    public static ZResult ok() {
        return new ZResult(null);
    }


    public static ZResult error(String msg) {
        return new ZResult(00,msg,null);
    }
    public static ZResult error() {
        return new ZResult(00,null,null);
    }


    public static ZResult build(Integer status, String msg) {
        return new ZResult(status, msg, null);
    }

    public ZResult() {

    }

    public ZResult(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ZResult(Object data) {
        this.status = 200;
        this.msg = "OK";
        this.data = data;
    }

//    public Boolean isOK() {
//        return this.status == Constant.Code.SUCCESS;
//    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

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

    public Object getData() {
        return data;
    }

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

    /**
     * 将json结果集转化为GResult对象
     *
     * @param jsonData json数据
     * @param clazz GResult中的object类型
     * @return
     */
    public static ZResult formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, ZResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                /*isObject:判段是否是对象*/
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                    /**/
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * Json to Bean
     * 没有object对象的转化
     * @param json
     * @return
     */
    public static ZResult json2Bean(String json) {
        try {
            return MAPPER.readValue(json, ZResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Json to List
     * Object是集合转化
     * @param jsonData json数据
     * @param clazz 集合中的类型
     * @return
     */
    public static ZResult json2List(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

}

相关文章

网友评论

      本文标题:springboot中可能使用的工具类

      本文链接:https://www.haomeiwen.com/subject/atjljktx.html