使用Hutool要注意了!升级到6.0后你调用的所有方法都将报错 ...

打印 上一主题 下一主题

主题 888|帖子 888|积分 2664

公众号赵侠客
  弁言

Hutool这个工具应该是家喻户晓了,它是一个开源的Java工具类库,提供了丰富的静态工具方法来简化Java开发中的常见代码。本日原来是想尝鲜体验一下Hutool新版本都更新了什么新功能,于是从5.x.x升到到了6.0.0,然后就出现了各种包名不存在的错误,我其时就蒙了。

图:Hutool升级6.0后报错

追念当年Mybatis-Plus从2.0升级到3.0时所有包名及方法都需要改一遍的痛苦经历仍然念兹在兹,至今还有许多项目在使用Mybatis-Plus2.0,因为升级修改的太多了,真的太痛苦了,干脆就不升级了。难道Hutool要重蹈Mybatis-plus的覆辙了?如果你使用了Hutool,以后想要升级到6.0时,那么你将要把所有的包名都改一遍,因为Hutool从5.0升级到6.0后包名全改了!!!

图:StrUtil在Hutool 5.0包名为cn.hutool.*


Hutool的作者Looly在gitee上也回应了6.0包名修改,说是为了兼容性考虑!!!

图:Hutool 作者回应更改包名

为什么改包名?

作者说更改包名是为了大改后能兼容,那为什么包名改成org.dromara?。在搞清为什么要将包名改为org.dromara前我们需要搞清晰这个dromara是什么?从官网先容来看dromara是一个聚集了一群开源爱好者的非赢利性构造,可以以为是中国版的Apache,那么就很好理解了,开源项目参加Apache那包名肯定要改成org.apache了。

图:Dromara官网

从Dromara 发布的消息来看,Hutool早在2021年3月15号就参加了该构造

图:Dromara宣布Hutool参加

Dromara社区旗下的顶级项目除了Hutool外还有许多我们耳熟能详的项目,我用过的就有:流程引擎框架LiteFlow、ElasticSearch客户端easy-es。

图:Dromara社区的顶级项目

现在各人应该明白Hutool为什么要把包名改成org.dromara了吧?
重要改了哪些


  • 包名修改,包名变更为org.dromara.hutool
  • 重新整理规整,内部工具类重新整理规整,淘汰无用模块,工具类归类到对应package中
  • 重构Http模块,被人诟病的http模块做重构,采用门面模式,支持包括HttpUrlConnection、HttpClient4、HttpClient5、OkHttp3等库。
  • 性能优化,在本事范围之内尽量做性能优化,不跟其他高性能库“攀比”。
  • 做减法,相比5.x版本做减法,大部门工作是删掉一些重复代码和无用的重载,使用上大概会增长代码量,但是相比淘汰了歧义
  • 统一构造方法,构建一种对象不再使用杂乱的createXXX、newXXX、ofXXX等名字,而是统一使用of大概ofXXX。
包名修改、重新整理规整

Util工具类修改了包名,不但将所有的cn.hutool改成org.dromara.hutool,还按工具类的用途做了分包, 如字符串类工具类移到了org.dromara.hutool.core.text.,聚集类的移到了 org.dromara.hutool.core.collection,时间的移到org.dromara.hutool.core.date*
5.0常用工具类
  1. cn.hutool.core.util.StrUtil.isEmpty("admin");
  2. cn.hutool.core.collection.CollUtil.isEmpty(new ArrayList<>());
  3. cn.hutool.core.date.DateUtil.now();
复制代码
6.0常用工具类
  1. org.dromara.hutool.core.text.StrUtil.isEmpty("admin");
  2. org.dromara.hutool.core.collection.CollUtil.isEmpty(new ArrayList<>());
  3. org.dromara.hutool.core.date.DateUtil.now();
复制代码
重构Http模块

5.0HttpUtill使用方法
  1. cn.hutool.core.util.StrUtil.isEmpty("admin");
  2. cn.hutool.core.collection.CollUtil.isEmpty(new ArrayList<>());
  3. cn.hutool.core.date.DateUtil.now();
复制代码
6.0使用了门面模式+依赖SPI机制提供可选引擎
  1. //默认使用 org.dromara.hutool.http.client.engine.httpclient5.HttpClient5Engine
  2. org.dromara.hutool.http.HttpUtil.get("https://www.baidu.com");   
  3.         
  4. final ClientEngine engine = new OkHttpEngine();
  5. final Request req = Request.of("https://www.baidu.com/").method(Method.GET);
  6. final Response res = engine.send(req);
  7. log.info(res.body().getString());
  8. final ClientEngine jdkHttpEngine = HttpUtil.createClient("jdkClient");
  9. final Response send = jdkHttpEngine.send(Request.of("https://www.baidu.com").method(Method.GET));
  10. log.info(send.body().getString());
  11. //目前支持4种HTTP引擎
  12. org.dromara.hutool.http.client.engine.httpclient5.HttpClient5Engine
  13. org.dromara.hutool.http.client.engine.httpclient4.HttpClient4Engine
  14. org.dromara.hutool.http.client.engine.okhttp.OkHttpEngine
  15. org.dromara.hutool.http.client.engine.jdk.JdkClientEngine
复制代码
更多别的不兼容修改

StrUtil

  1. - StrUtil.utf8Bytes(data));
  2. + ByteUtil.toUtf8Bytes(data));
复制代码
NumberUtil

  1. - import cn.hutool.core.util.NumberUtil;
  2. + import org.dromara.hutool.core.math.NumberUtil;
复制代码
ArrayUtil

  1. - import import cn.hutool.core.util.ArrayUtil;
  2. + import import org.dromara.hutool.core.array.ArrayUtil;
复制代码
ObjectUtil

  1. - cn.hutool.core.util.ObjectUtil.isNotEmpty(obj)
  2. + org.dromara.hutool.core.util.ObjUtil.isNotEmpty(obj)
复制代码
lambda整体结构、命名都规范了,以SerXxx开头

  1. - import cn.hutool.core.lang.func.Func1;
  2. + import org.dromara.hutool.core.func.SerFunction;
  3. - import cn.hutool.core.lang.Filter;
  4. + import java.util.function.Predicate;
复制代码
修改Base64

  1. - cn.hutool.core.codec.Base64Encoder.encode(encryptAesKey);
  2. + org.dromara.hutool.core.codec.binary.Base64.encode(encryptAesKey);
  3. - cn.hutool.core.codec.Base64Decoder.decode(pubKey);
  4. + org.dromara.hutool.core.codec.binary.Base64.decode(pubKey);
复制代码
generateKey

  1. - cn.hutool.crypto.SecureUtil.generateKey(str)
  2. + org.dromara.hutool.crypto.KeyUtil.generateKey(str)
复制代码
反射利用field

  1. - cn.hutool.core.util.ReflectUtil.getFields(clazz)
  2. + org.dromara.hutool.core.reflect.FieldUtil.getFields(clazz)
  3. - ReflectUtil.setFieldValue(clientPrincipal, "authorities", authorities);
  4. + FieldUtil.setFieldValue(clientPrincipal, "authorities", authorities);
复制代码
日期时间

  1. - cn.hutool.core.date.LocalDateTimeUtil
  2. - cn.hutool.core.date.DateUtil.date()
  3. - cn.hutool.core.date.DateUtil;
  4. + org.dromara.hutool.core.date.DateUtil;
  5. + org.dromara.hutool.core.date.DateUtil.now();
复制代码
dfa

  1. - import cn.hutool.dfa.WordTree;
  2. + import org.dromara.hutool.core.text.dfa.WordTree;
  3. - cn.hutool.core.text.replacer.ReplacerChain#replace(str)
  4. - org.dromara.hutool.core.text.replacer.ReplacerChain#apply(str)
复制代码
Dict

  1. - import cn.hutool.core.lang.Dict;
  2. + import org.dromara.hutool.core.map.Dict;
复制代码
JSONObject

  1. - jsonObject.putOpt(key,value).putOpt(key,value)
复制代码
UtilException

  1. - cn.hutool.core.exceptions.UtilException
复制代码
FileUtil

  1. - import cn.hutool.core.io.FileUtil;
  2. + import org.dromara.hutool.core.io.file.FileUtil;
  3. - FileUtil.exist(file))
  4. + FileUtil.exists(file))
  5. -FileUtil.cleanEmpty
  6. - cn.hutool.core.io.FileUtil.extName(filePath)
  7. + org.dromara.hutool.core.io.file.FileNameUtil.extName(filePath)
复制代码
CollUtil

  1. - cn.hutool.core.collection.CollUtil.newArrayList(obj);
  2. + org.dromara.hutool.core.collection.ListUtil.of(obj);
  3. - CollUtil.split(phones, 200);
  4. + CollUtil.partition(phones, 200);
  5. -ArrayUtil.toArray
复制代码
tree

  1. - cn.hutool.core.lang.tree.Tree;
  2. - cn.hutool.core.lang.tree.TreeNode;
  3. - cn.hutool.core.lang.tree.TreeUtil;
  4. + org.dromara.hutool.core.tree.MapTree;
  5. + org.dromara.hutool.core.tree.TreeNode;
  6. + org.dromara.hutool.core.tree.TreeUtil;
复制代码
create -> of

  1. - cn.hutool.json.JSONConfig.create()
  2. - Xxx.create()
  3. + org.dromara.hutool.json.JSONConfig.of()
  4. + Xxx.of()
复制代码
ReUtil

  1. - cn.hutool.core.util.ReUtil.RE_CHINESE;
  2. + org.dromara.hutool.core.regex.PatternPool.CHINESE;
复制代码
DefaultSegment

  1. - cn.hutool.core.lang.DefaultSegment#getStartIndex
  2. + org.dromara.hutool.core.lang.range.DefaultSegment#getBeginIndex
复制代码
http

  1. - cn.hutool.http.HttpUtil.post(url, data, timeout)
  2. + org.dromara.hutool.http.HttpGlobalConfig.setTimeout(TIMEOUT);
  3. + org.dromara.hutool.http.HttpUtil.post(url, data)
  4. - cn.hutool.http.HttpUtil.toParams(map)
  5. + org.dromara.hutool.core.net.url.UrlQueryUtil.toQuery(map)
复制代码
BeanUtil

  1. - cn.hutool.core.bean.BeanUtil;
  2. - cn.hutool.core.bean.copier.CopyOptions;
  3. + org.dromara.hutool.core.bean.BeanUtil;
  4. + org.dromara.hutool.core.bean.copier.CopyOptions;
复制代码
EnumUtil

  1. - cn.hutool.core.util.EnumUtil;
  2. + org.dromara.hutool.core.util.EnumUtil;
复制代码
IdUtil

  1. - cn.hutool.core.util.IdUtil;
  2. + org.dromara.hutool.core.data.id.IdUtil;
复制代码
RegexPool

  1. - cn.hutool.core.lang.RegexPool
  2. + org.dromara.hutool.core.regex.RegexPool
复制代码
URLDecoder

  1. - cn.hutool.core.net.URLDecoder;
  2. + org.dromara.hutool.core.net.url.URLDecoder;
复制代码
SpringUtil

  1. - cn.hutool.extra.spring.SpringUtil;
  2. + org.dromara.hutool.extra.spring.SpringUtil;
复制代码
SecureUtil

  1. - cn.hutool.crypto.SecureUtil;
  2. - cn.hutool.crypto.asymmetric.KeyType;
  3. - cn.hutool.crypto.symmetric.AES;
  4. - cn.hutool.core.util.HexUtil;
  5. - cn.hutool.crypto.BCUtil;
  6. - cn.hutool.crypto.SmUtil;
  7. - cn.hutool.crypto.asymmetric.SM2;
  8. - cn.hutool.crypto.symmetric.SM4;
  9. + org.dromara.hutool.crypto.SecureUtil;
  10. + org.dromara.hutool.crypto.asymmetric.KeyType;
  11. + org.dromara.hutool.crypto.symmetric.AES;
  12. + org.dromara.hutool.core.codec.HexUtil;
  13. + org.dromara.hutool.crypto.asymmetric.SM2;
  14. + org.dromara.hutool.crypto.symmetric.SM4;
  15. + org.dromara.hutool.crypto.bc.BCUtil;
  16. + org.dromara.hutool.crypto.bc.SmUtil;
复制代码
ImgUtil

  1. - cn.hutool.core.img.ImgUtil;
  2. + org.dromara.hutool.swing.img.ImgUtil;
复制代码
RandomUtil

  1. - cn.hutool.core.util.RandomUtil;
  2. + org.dromara.hutool.core.util.RandomUtil;
  3. - cn.hutool.core.io.IORuntimeException;
  4. - cn.hutool.system.SystemUtil;
  5. - RandomUtil.randomEleList(WORDS, wordCount);
  6. + RandomUtil.randomEles(WORDS, wordCount);
复制代码
Captcha

  1. - cn.hutool.core.img.FontUtil;
  2. - cn.hutool.captcha.CaptchaUtil;
  3. - cn.hutool.captcha.CircleCaptcha;
  4. - cn.hutool.captcha.GifCaptcha;
  5. - cn.hutool.captcha.LineCaptcha;
  6. - cn.hutool.captcha.ShearCaptcha
  7. + org.dromara.hutool.swing.captcha.CaptchaUtil;
  8. + org.dromara.hutool.swing.captcha.CircleCaptcha;
  9. + org.dromara.hutool.swing.captcha.GifCaptcha;
  10. + org.dromara.hutool.swing.captcha.LineCaptcha;
  11. + org.dromara.hutool.swing.captcha.ShearCaptcha
复制代码
IoUtil

  1. - cn.hutool.core.io.IoUtil;
  2. + org.dromara.hutool.core.io.IoUtil;
复制代码
sm2

  1. - (sm2.decryptFromBcd(content, KeyType.PrivateKey));
  2. + (sm2.decrypt(content, KeyType.PrivateKey));
  3. - sm2.encryptBcd(content, KeyType.PublicKey);
  4. + sm2.encryptHex(content, KeyType.PublicKey);
复制代码
最后

国产开源软件能靠自己活下来都挺不容易,被开源构造收编也是不错的出路,就是苦了我们这些开发者,全国使用Hutool的项目不在少数,针对这次重大改动本子提出一下几点发起:

  • 重度依赖,这种项目来说最好就不要改了,继续使用5.0,作者说了5.0也会继续维护
  • 轻度依赖,如果只是用了一些简单的工具类,全局替换包名升级照旧可以的
  • 新项目,我们需要思索有没有须要使用Hutool,是不是可以使用JDK17?许多新功能JDK都自带了,自己项目组是不是也应该维护起属于自己团队的工具类?

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

石小疯

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表