当前位置: 首页 > news >正文

后端项目开发:工具类封装(序列化、反射)

1.整合Jackson

根据《阿里巴巴开发规范》,包名使用单数,类名可以使用复数。
所以generic-common创建util包和utils工具类
很多时候我们需要将接收到的json数据转换为对象,或者将对象转为json存储。这时候我们需要编写用于json转换的工具类。
新建util目录,再创建JacksonUtils类


/*** JSON格式转换的工具类*/
public class JacksonUtils {private JacksonUtils() {}public final static ObjectMapper MAPPER;static {MAPPER = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_NULL);}public static String serialize(Object obj) {try {return MAPPER.writeValueAsString(obj);} catch (JsonProcessingException e) {e.printStackTrace();}return null;}public static Object deserialize(String jsonText, TypeReference type) {try {return MAPPER.readValue(jsonText, type);} catch (Exception e) {e.printStackTrace();}return null;}public static <T> T deserialize(String jsonText, Class<T> beanClass) {try {return MAPPER.readValue(jsonText, beanClass);} catch (Exception e) {e.printStackTrace();}return null;}public static JsonNode deserialize(String jsonText) {try {return MAPPER.readTree(jsonText);} catch (Exception e) {e.printStackTrace();}return null;}
}

2.反射工具类ReflectionUtils


/*** 反射相关方法*/
public class ReflectionUtils {/*** 根据方法名调用指定对象的方法* @param object 要调用方法的对象* @param method 要调用的方法名* @param args 参数对象数组* @return*/public static Object invoke(Object object, String method, Object... args) {Object result = null;Class<? extends Object> clazz = object.getClass();Method queryMethod = getMethod(clazz, method, args);if(queryMethod != null) {try {result = queryMethod.invoke(object, args);} catch (IllegalAccessException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}} else {try {throw new NoSuchMethodException(clazz.getName() + " 类中没有找到 " + method + " 方法。");} catch (NoSuchMethodException e) {e.printStackTrace();}}return result;}/*** 根据方法名和参数对象查找方法* @param clazz* @param name* @param args 参数实例数据* @return*/public static Method getMethod(Class<? extends Object> clazz, String name, Object[] args) {Method queryMethod = null;Method[] methods = clazz.getMethods();for(Method method:methods) {if(method.getName().equals(name)) {Class<?>[] parameterTypes = method.getParameterTypes();if(parameterTypes.length == args.length) {boolean isSameMethod = true;for(int i=0; i<parameterTypes.length; i++) {Object arg = args[i];if(arg == null) {arg = "";}if(!parameterTypes[i].equals(args[i].getClass())) {isSameMethod = false;}}if(isSameMethod) {queryMethod = method;break ;}}}}return queryMethod;}
}
http://www.lryc.cn/news/137889.html

相关文章:

  • 软件测试技术分享丨遇到bug怎么分析?
  • LeetCode无重复字符的最长子串
  • 17.2.2 【Linux】通过systemctl观察系统上所有的服务
  • Redis扩容机制与一致性哈希算法解析
  • BDA初级分析——可视化基础
  • 边缘计算节点BEC典型实践:如何快速上手PC-Farm服务器?
  • python自动把内容发表到wordpress完整示例及错误解答
  • 【javaweb】学习日记Day6 - Mysql 数据库 DDL DML DQL
  • 如何利用SFTP如何实现更安全的远程文件传输 ——【内网穿透】
  • 枚举和反射
  • MinIO【部署 01】MinIO安装及SpringBoot集成简单测试
  • 问道管理:证券代码是什么?有什么用?
  • 中文医学知识语言模型:BenTsao
  • Java基础十四(字符串)
  • vue3 基础知识 (动态组件 keep-alive 等) 04
  • 【C# Programming】编程入门:数组、操作符、控制流
  • 线上问诊:业务数据采集
  • 2023 CCPC 华为云计算挑战赛 hdu7399 博弈,启动!(图上博弈/枚举+逆向有向图sg函数)
  • Unity之 Vector3 的详细介绍以及方法的介绍
  • Postgresql部署及简单操作
  • rabbitmq集群搭建
  • C++ 二叉搜索树的概念特性
  • 7、Spring_AOP
  • QChart:数据可视化(用图像形式显示数据内容)
  • 【python】Leetcode(primer-set)
  • 【LVS集群】
  • 软考高级系统架构设计师系列之:论文题目类型、论文考试大纲、历年考试论文真题汇总、论文写作原则、论文写作常见问题、论文评分标准
  • 完整的application.xml
  • C语言:运算符优先级
  • Android GreenDao数据库升级(附Demo)