SpringBoot中如何使用ObjectMapper,老鸟们都是这样玩的?

开发 前端
使用单例模式进行字符串转对象时性能可以提升18倍,而对象转String性能快了惊人的290万倍,所以在Spring中如何正确的使用ObjectMapper不用我再说了吧~

1. 每次new一个

在SpringBoot项目中要实现对象与Json字符串的互转,每次都需要像如下一样new 一个ObjectMapper对象:

public UserEntity string2Obj(String json) throws JsonProcessingException {
 ObjectMapper objectMapper = new ObjectMapper();
 return objectMapper.readValue(json, UserEntity.class);
}

public String obj2String(UserEntity userEntity) throws JsonProcessingException {
 ObjectMapper objectMapper = new ObjectMapper();
 return objectMapper.writeValueAsString(car)
}

这样的代码到处可见,有问题吗?

你要说他有问题吧,确实能正常执行;可你要说没问题吧,在追求性能的同学眼里,这属实算是十恶不赦的代码了。

首先,让我们用JMH对这段代码做一个基准测试,让大家对其性能有个大概的了解。

基准测试是指通过设计科学的测试方法、测试工具和测试系统,实现对一类测试对象的某项性能指标进行定量的和可对比的测试。而JMH是一个用来构建,运行,分析Java或其他运行在JVM之上的语言的 纳秒/微秒/毫秒/宏观 级别基准测试的工具。

@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Thread)
@Fork(1)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 3, time = 1)
public class JsonJMHTest {
    
    String json = "{\"id\":122345667,\"email\":\"jianzh5@163.com\",\"price\":12.25}";
    UserEntity userEntity = new UserEntity(13345L,"jianzh5@163.com", BigDecimal.valueOf(12.25));
    /**
     * 测试String to Object
     */
    @Benchmark
    public UserEntity objectMapper2ObjTest() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(json, UserEntity.class);
    }

    /**
     * 测试Object to String
     */
    @Benchmark
    public String objectMapper2StringTest() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(userEntity);
    }
  
   public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(JsonJMHTest.class.getSimpleName())
                .build();
        new Runner(opt).run();
    }
}

测试环境

# JMH version: 1.36
# VM version: JDK 17.0.3, OpenJDK 64-Bit Server VM, 17.0.3+7-LTS
# Mac AppleM1/16GB

测试结果

图片图片

通过测试结果可以看出,每次new一个ObjectMapper,在实现字符串转对象时每秒可以完成23万多次,而实现对象转Json字符串每秒仅可完成2.7万次。

那该如何优化,提升性能呢?

2. 单例化

老鸟们都知道,在创建工具类时要将工具类设置成单例的,这样不仅可以保证线程安全,也可以保证在系统全局只能创建一个对象,避免频繁创建对象的成本。

所以,我们可以在项目中构建一个ObjectMapper的单例类。

@Getter
public enum ObjectMapperInstance {
    
    INSTANCE;
    
    private final ObjectMapper objectMapper = new ObjectMapper();
    
    ObjectMapperInstance() {
        
    }
}

再次使用JMH对其测试:

@Benchmark
public UserEntity singleten2ObjTest() throws JsonProcessingException {
  ObjectMapper objectMapper = ObjectMapperInstance.INSTANCE.getObjectMapper();
  return objectMapper.readValue(json, UserEntity.class);
}

@Benchmark
public String singleten2StringTest() throws JsonProcessingException {
  ObjectMapper objectMapper = ObjectMapperInstance.INSTANCE.getObjectMapper();
  return objectMapper.writeValueAsString(userEntity);
}

测试结果如下:

图片图片

可以看到,使用单例模式,String转对象的方法每秒可以执行420多万次,比new ObjectMapper的方式快了18倍;而对象转String的方法每秒可以执行830万次,性能提升了300倍(看到结果的一瞬间我傻眼了,一度怀疑是写错代码了)!!!!

3. 个性化配置

当然,在项目中使用ObjectMapper时,有时候我们还需要做一些个性化配置,比如将Long和BigDemical类型的属性都通过字符串格式进行转换,防止前端使用时丢失数值精度。

这些类型转换的格式映射都可以在单例类中配置,代码如下:

@Getter
public enum ObjectMapperInstance {
    
    INSTANCE;
    
    private final ObjectMapper objectMapper;
    
    ObjectMapperInstance() {
        objectMapper = new ObjectMapper();
        // 注册自定义模块
        initialize();
    }
    
    private void initialize() {
        CustomJsonModule customJsonModule = new CustomJsonModule();
        objectMapper.registerModule(customJsonModule);
    }
}

在initialize()方法中给ObjectMapper注册自定义序列化转换器。

图片图片

第一行是使用注册自定义序列换转换器后的效果,给id和price字段都加上了引号。

再来一次JMH测试:

图片图片

可以看到,给ObjectMapper额外注册转换类型以后性能会受到一定的影响,但对业务影响不大。(啥业务能这么高的请求~)

4. 小结

通过上面的测试,结论已经很清晰了。使用单例模式进行字符串转对象时性能可以提升18倍,而对象转String性能快了惊人的290万倍,所以在Spring中如何正确的使用ObjectMapper不用我再说了吧~

责任编辑:武晓燕 来源: JAVA日知录
相关推荐

2023-04-12 08:56:37

RocketMQSpring核心业务

2021-07-16 08:58:35

SpringBoot

2023-02-04 10:08:40

2015-07-15 13:47:13

上网高手

2020-11-16 13:38:31

PostMessage

2021-07-28 06:10:47

拖拽设计器 transmat

2021-09-05 07:55:37

前端Emoji 表情

2019-05-30 14:58:56

Pythonxml文件

2014-07-15 11:02:20

2018-08-27 15:53:43

编程语言javaPython

2013-08-22 10:28:50

.NET MVC.NETRazor

2023-11-30 22:54:15

2018-05-20 11:25:45

520程序员表白

2016-12-28 14:51:46

大数据应用

2015-07-14 11:01:39

CSS科技公司

2022-07-24 14:51:31

开源游戏RPG 游戏

2021-04-02 06:05:31

Windows 10Windows操作系统

2011-03-09 10:39:13

LAMP简介

2018-01-08 07:34:39

比特币数字货币央行

2019-01-29 10:00:59

GitHub开源搜索
点赞
收藏

51CTO技术栈公众号