0
点赞
收藏
分享

微信扫一扫

java 小工具 (四)


文章目录

  • ​​1.ReflectionUtils​​
  • ​​1.1 获取方法​​
  • ​​1.2 获取字段​​
  • ​​1.3 执行方法​​
  • ​​1.4 判断字段是否常量​​
  • ​​1.5 判断是否equals方法​​
  • ​​2.Base64Utils​​
  • ​​2.1 加密方法encode​​
  • ​​2.2 解密decode​​
  • ​​3.StandardCharsets​​
  • ​​4.DigestUtils​​
  • ​​4.1 md5加密​​
  • ​​4.2 sha256加密​​
  • ​​5.SerializationUtils​​
  • ​​6.HttpStatus​​

1.ReflectionUtils

org.springframework.util下

1.1 获取方法

Method method = ReflectionUtils.findMethod(User.class, "getId");

1.2 获取字段

Field field = ReflectionUtils.findField(User.class, "id");

1.3 执行方法

.invokeMethod()

@Service
public class ReInvokeService {
@Autowired
private SpringContextsUtil springContextsUtil;

public void reInvoke(String beanName,String methodName,String[] params){
Method method = ReflectionUtils.findMethod(springContextsUtil.getBean(beanName).getClass(), methodName, String.class, String.class, Boolean.class,String.class);
Object[] param1 = new Object[3];
param1[0]=params[0];
param1[1]=params[1];
param1[2]=true;
ReflectionUtils.invokeMethod(method, springContextsUtil.getBean(beanName), param1);
}
}

1.4 判断字段是否常量

Field field = ReflectionUtils.findField(User.class, "id");
System.out.println(ReflectionUtils.isPublicStaticFinal(field));

1.5 判断是否equals方法

Method method = ReflectionUtils.findMethod(User.class, "getId");
System.out.println(ReflectionUtils.isEqualsMethod(method));

java 小工具 (四)_java

2.Base64Utils

为了安全考虑,需要将参数只用base64编码

org.springframework.util下面的

2.1 加密方法encode

String str = "abc";
String encode = new String(Base64Utils.encode(str.getBytes()));
System.out.println("加密后:" + encode);
加密后:YWJj

2.2 解密decode

String str = "abc";
String encode = new String(Base64Utils.encode(str.getBytes()));
System.out.println("加密后:" + encode);
try {
String decode = new String(Base64Utils.decode(encode.getBytes()), "utf8");
System.out.println("解密后:" + decode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
加密后:YWJj
解密后:abc

3.StandardCharsets

指定字符编码,比如:UTF-8、ISO-8859-1

java.nio.charset.StandardCharsets

String str = "abc";
String encode = new String(Base64Utils.encode(str.getBytes()));
System.out.println("加密后:" + encode);
String decode = new String(Base64Utils.decode(encode.getBytes())
, StandardCharsets.UTF_8);
System.out.println("解密后:" + decode);

4.DigestUtils

md5加密
sha256

4.1 md5加密

String md5Hex = DigestUtils.md5Hex("1");

4.2 sha256加密

String md5Hex = DigestUtils.sha256Hex("1");

java 小工具 (四)_intellij-idea_02

5.SerializationUtils

序列化和反序列化处理

org.springframework.util.SerializationUtils

Map<String, String> map = Maps.newHashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
byte[] serialize = SerializationUtils.serialize(map);
Object deserialize = SerializationUtils.deserialize(serialize);
System.out.println(deserialize);

6.HttpStatus

接口正常返回200,异常返回500,接口找不到返回404,接口不可用返回502等。

private int SUCCESS_CODE = 200;
private int ERROR_CODE = 500;
private int NOT_FOUND_CODE = 404;

org.springframework.http.HttpStatus

java 小工具 (四)_intellij-idea_03

举报

相关推荐

0 条评论