Bean转换工具类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Slf4j
public class BeanHelper {
public static <T> T copyProperties(Object source, Class<T> target){
try {
T t = target.newInstance();
BeanUtils.copyProperties(source, t);
return t;
} catch (Exception e) {
log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e);
//throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR);
return null;
}
}
public static <T> List<T> copyWithCollection(List<?> sourceList, Class<T> target){
try {
return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toList());
} catch (Exception e) {
log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e);
//throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR);
return null;
}
}
public static <T> Set<T> copyWithCollection(Set<?> sourceList, Class<T> target){
try {
return sourceList.stream().map(s -> copyProperties(s, target)).collect(Collectors.toSet());
} catch (Exception e) {
log.error("【数据转换】数据转换出错,目标对象{}构造函数异常", target.getName(), e);
//throw new LyException(ExceptionEnum.DATA_TRANSFER_ERROR);
return null;
}
}
}
正则校验
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexUtils {
/**
* 手机号正则
*/
public static final String PHONE_REGEX = "^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$";
/**
* 邮箱正则
*/
public static final String EMAIL_REGEX = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
/**
* 是否符合手机格式
*
* @param phone 要校验的手机号
* @return true:符合,false:不符合
*/
public static boolean isPhone(String phone) {
return matches(phone, PHONE_REGEX);
}
/**
* 是否符合邮箱格式
*
* @param email 要校验的邮箱
* @return true:符合,false:不符合
*/
public static boolean isEmail(String email) {
return matches(email, EMAIL_REGEX);
}
private static boolean matches(String str, String regex) {
if (StringUtils.isBlank(str)) {
return false;
}
return str.matches(regex);
}
/**
* 验证用户名,支持中英文(包括全角字符)、数字、下划线和减号 (全角及汉字算两位),长度为4-20位,中文按二位计数
*
* @param userName
* @return
*/
public static boolean validateUserName(String userName) {
String validateStr = "^[\\w\\--_[0-9]\u4e00-\u9fa5\uFF21-\uFF3A\uFF41-\uFF5A]+$";
boolean rs = false;
rs = matcher(validateStr, userName);
if (rs) {
int strLenth = getStrLength(userName);
if (strLenth < 4 || strLenth > 20) {
rs = false;
}
}
return rs;
}
/**
* 获取字符串的长度,对双字符(包括汉字)按两位计数
*
* @param value
* @return
*/
public static int getStrLength(String value) {
int valueLength = 0;
String chinese = "[\u0391-\uFFE5]";
for (int i = 0; i < value.length(); i++) {
String temp = value.substring(i, i + 1);
if (temp.matches(chinese)) {
valueLength += 2;
} else {
valueLength += 1;
}
}
return valueLength;
}
private static boolean matcher(String reg, String string) {
boolean tem = false;
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(string);
tem = matcher.matches();
return tem;
}
public static void main(String[] args) {
String str = "0-_f9zd中22";
String st = "A-dq_!!!!去符号标号!ノチセたのひちぬ!当然。!!..**半角";
System.out.println(validateUserName(str));
System.out.println(st.replaceAll("[\\pP&&[^-_]]", ""));
System.out.println(st.replaceAll("[\\w\\-一-龥A-Za-z]", ""));
System.out.println("===========================");
System.out.println(checkPassWord("aaa_123&"));
System.out.println(getStrLength("贾彭飞"));
}
/**
* 密码复杂度校验(同时包含数字,字母,特殊符号)
*
* @param val
* @return
*/
public static boolean checkPassWord(String val) {
String reg = "^^(?![a-zA-z]+$)(?!\\d+$)(?![!@#$%^&*_-]+$)(?![a-zA-z\\d]+$)(?![a-zA-z!@#$%^&*_-]+$)(?![\\d!@#$%^&*_-]+$)[a-zA-Z\\d!@#$%^&*_-]+$";
return val.matches(reg);
}
}
Json工具类
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class JsonUtils {
public static final ObjectMapper mapper = new ObjectMapper();
private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
public static String toString(Object obj) {
if (obj == null) {
return null;
}
if (obj.getClass() == String.class) {
return (String) obj;
}
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
logger.error("json序列化出错:" + obj, e);
return null;
}
}
public static <T> T toBean(String json, Class<T> tClass) {
try {
return mapper.readValue(json, tClass);
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
public static <E> List<E> toList(String json, Class<E> eClass) {
try {
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
public static <K, V> Map<K, V> toMap(String json, Class<K> kClass, Class<V> vClass) {
try {
return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
public static <T> T nativeRead(String json, TypeReference<T> type) {
try {
return mapper.readValue(json, type);
} catch (IOException e) {
logger.error("json解析出错:" + json, e);
return null;
}
}
public static void main(String[] args) {
//序列化 bean -》 json
User jack = new User("jack", 20);
String jsonStr = toString(jack);
System.out.println(jsonStr);
//序列化 list -> json
List<User> users = Arrays.asList(jack, new User("rose", 19));
jsonStr = toString(users);
System.out.println(jsonStr);
//反序列化
String json = "{\"name\":\"jack\",\"age\":20}";
User jackClass = toBean(json, User.class);
System.out.println("user="+jackClass);
String jsons = "[{\"name\":\"jack\",\"age\":20},{\"name\":\"rose\",\"age\":19}]";
List<User> users1 = toList(jsons, User.class);
System.out.println("userlist = "+users1);
String mapJsons = "{\"65\":[{\"name\":\"jack\",\"age\":20},{\"name\":\"rose\",\"age\":19}],\"66\":[{\"name\":\"jack\",\"age\":20},{\"name\":\"rose\",\"age\":19}]}";
Map<String, List<User>> stringListMap = nativeRead(mapJsons, new TypeReference<Map<String, List<User>>>() {});
System.out.println(stringListMap);
}
@Data
@AllArgsConstructor
@NoArgsConstructor
static class User{
private String name;
private int age;
}
}
标签:return,String,json,static,import,常用工具,public
From: https://www.cnblogs.com/anhk/p/17176839.html