栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 小工具 (四)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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));

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");

5.SerializationUtils

序列化和反序列化处理

org.springframework.util.SerializationUtils

Map 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/948570.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号