• EntityNotFoundException EntityExistException


    package me.zhengjie.common.exception;
    
    import org.springframework.util.StringUtils;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.stream.IntStream;
    
    /**
     * @author jie
     * @date 2018-11-23
     */
    public class EntityNotFoundException extends RuntimeException {
    
        public EntityNotFoundException(Class clazz, Object... searchParamsMap) {
            super(EntityNotFoundException.generateMessage(clazz.getSimpleName(), toMap(String.class, String.class, searchParamsMap)));
        }
    
        private static String generateMessage(String entity, Map<String, String> searchParams) {
            return StringUtils.capitalize(entity) +
                    " 不存在 " +
                    searchParams;
        }
    
        private static <K, V> Map<K, V> toMap(
                Class<K> keyType, Class<V> valueType, Object... entries) {
            if (entries.length % 2 == 1)
                throw new IllegalArgumentException("Invalid entries");
            return IntStream.range(0, entries.length / 2).map(i -> i * 2)
                    .collect(HashMap::new,
                            (m, i) -> m.put(keyType.cast(entries[i]), valueType.cast(entries[i + 1])),
                            Map::putAll);
        }
    
    }
    package me.zhengjie.common.exception;
    
    import org.springframework.util.StringUtils;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.stream.IntStream;
    
    /**
     * @author jie
     * @date 2018-11-23
     */
    public class EntityExistException extends RuntimeException {
    
        public EntityExistException(Class clazz, Object... saveBodyParamsMap) {
            super(EntityExistException.generateMessage(clazz.getSimpleName(), toMap(String.class, String.class, saveBodyParamsMap)));
        }
    
        private static String generateMessage(String entity, Map<String, String> saveBodyParams) {
            return StringUtils.capitalize(entity) +
                    " 已存在 " +
                    saveBodyParams;
        }
    
        private static <K, V> Map<K, V> toMap(
                Class<K> keyType, Class<V> valueType, Object... entries) {
            if (entries.length % 2 == 1)
                throw new IllegalArgumentException("Invalid entries");
            return IntStream.range(0, entries.length / 2).map(i -> i * 2)
                    .collect(HashMap::new,
                            (m, i) -> m.put(keyType.cast(entries[i]), valueType.cast(entries[i + 1])),
                            Map::putAll);
        }
    }
    throw new EntityNotFoundException(User.class, "name", username);
  • 相关阅读:
    mysql原生语句基础知识
    利用layui前端框架实现对不同文件夹的多文件上传
    简述layui前端ui框架的使用
    利用bootstrap-select.min.js实现bootstrap下拉列表的单选和多选
    使用pycharm进行远程开发部署调试设置 与 远程部署调试是否必须使用远程主机的解释器?
    博客园积分规则
    mysql 数据库的备份与恢复
    flask 利用flask_wtf扩展 创建web表单
    jquery ajax几种书写方式的总结
    LightSpeed 的Left Join Bug解决方案
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11216969.html
Copyright © 2020-2023  润新知