常用API Utils:
// 略略略略
自定义Utils:
ValidationUtils
public class ValidationUtils {
private ValidationUtils() {
}
/**
* 验证器
*/
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
/**
* 校验实体,返回实体所有属性的校验结果
*/
public static <T> void validateEntity(T obj, Class<?> group) {
//解析校验结果
Set<ConstraintViolation<T>> validateSet = validator.validate(obj, group);
buildValidationResult(validateSet);
}
/**
* 校验指定实体的指定属性是否存在异常
*/
public static <T> void validateProperty(T obj, String propertyName, Class<?> group) {
Set<ConstraintViolation<T>> validateSet = validator.validateProperty(obj, propertyName, group);
buildValidationResult(validateSet);
}
/**
* 将异常结果封装返回
* @param validateSet 所有的不满足的校验信息
*/
private static <T> void buildValidationResult(Set<ConstraintViolation<T>> validateSet) {
ValidationResult validationResult = new ValidationResult();
if (CollectionUtils.isNotEmpty(validateSet)) {
validationResult.setHasErrors(true);
Map<String, String> errorMsgMap = new HashMap<>();
for (ConstraintViolation<T> constraintViolation : validateSet) {
errorMsgMap.put(constraintViolation.getPropertyPath().toString(), constraintViolation.getMessage());
}
validationResult.setErrorMsg(errorMsgMap);
}
if (validationResult.isHasErrors()){
throw new BusinessException(validationResult.getMessage());
}
}
}
JsonUtils:
public class JsonUtils {
private JsonUtils() {
}
private static final Gson gson = new Gson();
// // 使用GsonBuilder指定时间格式,避免服务器端出现Invalid time zone indicator异常
// private static final Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
/**
* 对象toJson
*/
public static <T> String toJson(T object) {
return gson.toJson(object);
}
/**
* JsonTo对象
* json为null或者""则返回null,json为"{}"则返回对象,但成员变量都为null
*/
public static <T> T toObj(String json, Class<T> clazz) {
return gson.fromJson(json, clazz);
}
/**
* 数组JsonTo List
*/
public static <T> List<T> toList(String json, Class<T> clazz) {
return gson.fromJson(json, TypeToken.getParameterized(List.class,clazz).getType());
}
/**
* Map
*/
public static <K, V> Map<K, V> toMap(String json, Type type) {
return gson.fromJson(json, type);
}
/**
* 获取Map<K,V> Type
*/
public static <K, V> Type getMapType(Class<K> kClazz, Class<V> vClazz) {
return TypeToken.getParameterized(Map.class, kClazz, vClazz).getType();
}
/**
* MapToJson
*/
public static String toJson(Object obj, Type type) {
return gson.toJson(obj, type);
}
}
SpringUtils(Bean)
@Order(Ordered.HIGHEST_PRECEDENCE)
@Component
@Slf4j
public class SpringUtils implements ApplicationContextAware {
public static ApplicationContext getContext() {
return context;
}
private static ApplicationContext context;
public static void setContext(ApplicationContext context) {
SpringUtils.context = context;
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static Object getBean(String name) {
return context.getBean(name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
}
CacheUtils(Redis)
public class CacheUtils {
@Autowired
StringRedisTemplate stringRedisTemplate;
private static StringRedisTemplate redisTemplate;
private static ValueOperations<String, String> opsForValue;
@PostConstruct
public void init() {
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
redisTemplate = stringRedisTemplate;
opsForValue = stringStringValueOperations;
}
/**
* 根据Key获取value。
* 适配 redis 的 get 命令。
* 如果存储时为json字符串,则获取到value后需自行进行json的反序列化。
* @param key
* @return
*/
public static String get(String key) {
String value = opsForValue.get(key);
return value;
}
/**
* 键值数据放入缓存。
* 依赖于 redis set 命令。
* @param key
* @param value
* @param num
* @param timeUnit
*/
public static void set(String key, String value, long num, TimeUnit timeUnit) {
opsForValue.set(key, value, num, timeUnit);
}
/**
* 键值数据放入缓存,缓存时长为 seconds 秒。
* 依赖于 redis set 命令。
* @param key
* @param value
* @param seconds
*/
public static void set(String key, String value, long seconds) {
opsForValue.set(key, value, seconds, TimeUnit.SECONDS);
}
/**
* 删除 key。
* @param key
*/
public static void del(String key) {
redisTemplate.delete(key);
}
/**
* 批量删除 key。
* @param list
*/
public static void del(Collection<String> list) {
redisTemplate.delete(list);
}
/**
* 根据前缀清空缓存
* @param cacheAmcResourceKey
*/
public static void delAll(String cacheAmcResourceKey) {
final Set<String> keys = redisTemplate.keys(cacheAmcResourceKey+"*");
if (keys != null && !keys.isEmpty()) {
redisTemplate.delete(keys);
}
}
}