zoukankan      html  css  js  c++  java
  • Springboot单例模式实战封装json转换

    一、定义

            保证一个类仅有一个实例,并提供一个全局访问点.

    二、优点

          (1)在内存里只有一个实例,减少了内存开销
          (2)可以避免对资源的多重占用
          (3)设置全局访问点,严格控制访问 (对外不会new出来,只能通过这个方法创建对象)

    三、缺点

         没有接口,扩展困难.

    四、举例实现,实际中常用

         

    封装成转换json的类,传入一个对象,转换为json形式,就可以封装成一个util

            最好的实现方式是枚举的实现方式。

    一、枚举的实现方式:

      1 @Slf4j
      2 public enum EnumJacksonUtil {
      3 
      4     /**
      5      * 方法集合
      6      */
      7     INSTANCE {
      8 
      9         /**
     10          * 转成json
     11          * @param object 传入的实体类
     12          * @return
     13          */
     14         @Override
     15         public String toJsonString(Object object) {
     16             String json = null;
     17             if (!StringUtils.isEmpty(object)) {
     18                 try {
     19                     log.info("传入对象:" + object);
     20                     json = mapper.writeValueAsString(object);
     21                     log.info("转换结果:" + json);
     22                 } catch (JsonProcessingException e) {
     23                     log.info("json转换异常{}" + object);
     24                     e.getMessage();
     25                 }
     26             }
     27             return json;
     28         }
     29 
     30         /**
     31          *
     32          * @param jsonStr 传入的字符串
     33          * @param cls   传入的实体类
     34          * @return
     35          */
     36         @Override
     37         public <T> T stringToBean(String jsonStr, Class<T> cls) {
     38             T t = null;
     39             if (!StringUtils.isEmpty(mapper)) {
     40                 try {
     41                     mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
     42                     mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
     43                     t = mapper.readValue(jsonStr, cls);
     44                 } catch (IOException e) {
     45                     log.info("json转换异常{}" + jsonStr);
     46                     e.getMessage();
     47                 }
     48             }
     49             return t;
     50         }
     51 
     52         /**
     53          * 将json数据转换成pojo对象list
     54          * @param jsonData
     55          * @param beanType
     56          * @param <T>
     57          * @return
     58          */
     59         @Override
     60         public <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
     61             JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, beanType);
     62             try {
     63                 List<T> list = mapper.readValue(jsonData, javaType);
     64                 return list;
     65             } catch (Exception e) {
     66                 e.printStackTrace();
     67             }
     68 
     69             return null;
     70         }
     71     };
     72 
     73     /**
     74      * springboot转换json的类
     75      */
     76     private static ObjectMapper mapper;
     77 
     78     static {
     79         mapper = new ObjectMapper();
     80     }
     81 
     82     /**
     83      * 转成json
     84      *
     85      * @param object 传入的实体类
     86      * @return
     87      */
     88     public abstract String toJsonString(Object object);
     89 
     90     /**
     91      * 转成bean
     92      *
     93      * @param jsonStr 传入的字符串
     94      * @param cls     传入的实体类
     95      * @return
     96      */
     97     public abstract <T> T stringToBean(String jsonStr, Class<T> cls);
     98 
     99     /**
    100      * 将json数据转换成pojo对象list
    101      *
    102      * @param jsonData
    103      * @param beanType
    104      * @param <T>
    105      * @return
    106      */
    107     public abstract <T> List<T> jsonToList(String jsonData, Class<T> beanType);
    108 
    109 
    110     public static EnumJacksonUtil getInstance() {
    111         return INSTANCE;
    112     }
    113 
    114 }
    View Code

         测试方式:

     1 @RunWith(SpringRunner.class)
     2 @SpringBootTest
     3 public class EnumApplicationTests {
     4 
     5     @Test
     6     public void contextLoads() {
     7 
     8         Thermo thermo = new Thermo( );
     9         thermo.setName("塞米菲");
    10         thermo.setDescribe("一起一个");
    11 
    12         EnumJacksonUtil enumJacksonUtil = EnumJacksonUtil.getInstance();
    13         System.out.println( enumJacksonUtil.toJsonString(thermo));
    14     }
    15 
    16 }
    View Code

    二、一般单例的实现方式

     1 @Slf4j
     2 public class JacksonUtil {
     3 
     4     private static ObjectMapper mapper;
     5 
     6     static {
     7         //noinspection ConstantConditions
     8         if (mapper == null) {
     9             mapper = new ObjectMapper();
    10         }
    11     }
    12 
    13     /**
    14      * 转成json
    15      *
    16      * @param object
    17      * @return
    18      */
    19     public static String toJsonString(Object object) {
    20         String json = null;
    21         if (object != null) {
    22             try {
    23                 json = mapper.writeValueAsString(object);
    24             } catch (JsonProcessingException e) {
    25                 log.info("json转换异常{}" + object);
    26                 e.getMessage();
    27             }
    28         }
    29         return json;
    30     }
    31 
    32     /**
    33      * 转成bean
    34      *
    35      * @param jsonStr
    36      * @param cls
    37      * @return
    38      */
    39     public static <T> T stringToBean(String jsonStr, Class<T> cls) {
    40         T t = null;
    41         if (mapper != null) {
    42             try {
    43                 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    44                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    45                 t = mapper.readValue(jsonStr, cls);
    46             } catch (IOException e) {
    47                 e.getMessage();
    48             }
    49         }
    50         return t;
    51     }
    52 }
    View Code

          测试方法:

    1 @Test
    2     public void contextLoad2s() {
    3 
    4         Thermo thermo = new Thermo( );
    5         thermo.setName("塞米菲");
    6         thermo.setDescribe("一起一个");
    7         
    8         System.out.println(JacksonUtil.toJsonString(thermo));
    9     }
    View Code

       

  • 相关阅读:
    vbox安装增强功能,实现宿主机文件夹共享并浏览器访问
    linux镜像下载
    linux命令之sed
    关于MySQL数据库的备份方案
    linux防火墙使用以及配置
    Jenkins安装部署(二)
    Jenkins安装部署(一)
    Centos7在虚拟机中扩展磁盘空间
    CentOS 7系统根目录分区扩容
    Linux下的SVN服务器搭建
  • 原文地址:https://www.cnblogs.com/liuyangfirst/p/10436136.html
Copyright © 2011-2022 走看看