当前位置:网站首页>Jackson的ObjectMapper在项目中的主要运用
Jackson的ObjectMapper在项目中的主要运用
2022-08-10 03:23:00 【头顶凉凉先生丶】
【项目】Jackson的ObjectMapper在项目中的主要运用
1、对象序列化:【对象的状态信息转换为可以存储或传输的形式的过程【Json数据】】
ObjectMapper objectMapper = new ObjectMapper();
User user = new User();
String jsonValue = objectMapper.writeValueAsString(user);
2、反序列化:【Json字符串进行反向操纵、生成对应的实体类等信息】 这里以流为例子。
ObjectMapper objectMapper = new ObjectMapper();
FileInputStream fileInputStream = new FileInputStream(*);
objectMapper.readValue(fileInputStream,User.class);
其他传入形式:

3、指定前端显示日期格式
【业务分析:后端存入的时间数据往往是 2021-05-27 08:00:00 具体数据、一般前端显示不需要整个数据、所以需要对时间数据进行固定显示。 前后端都可解决此问题。 前端一般拆分每个对应数据(时分秒等)做拼接、后端则可通过工具进行传输对应格式数据】
4、前端传入Long类型数据确实精度问题解决
【业务分析:当长整型数据传给前端(通常主键雪花算法生成)、Js处理数据返回时、会出现精度缺失(后几位变为0)、这里一般也分为前后端解决】
前端解决:利用bignumber.js 按千分位格式话数据(不会有精度问题、不会有溢出问题)
后端解决:通常主键雪花算法生成ID呈64位、如果用Long传输、则会出现问题。那么我们可以采取兼容性最强的String类型进行定义数据类型。
择中解决:利用Jackson提供的ObjectMapper将数据转化成String类型
创建工具类
package com.itcast.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
public class JacksonObjectMapper extends ObjectMapper {
//指定页面显示的日期格式
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public JacksonObjectMapper() {
super();
//收到未知属性时不报异常
this.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
//反序列化时,属性不存在的兼容处理
this.getDeserializationConfig().withoutFeatures(FAIL_ON_UNKNOWN_PROPERTIES);
//自定义转换规则(自己去指定)
//长整型 Long 日期date
SimpleModule simpleModule = new SimpleModule()
.addSerializer(BigInteger.class, ToStringSerializer.instance)//将BigInteger转换为String
.addSerializer(Long.class, ToStringSerializer.instance)//将Long转换成String
.addSerializer(Date.class, new DateSerializer(false, new SimpleDateFormat(DEFAULT_DATE_FORMAT))); // 将Date转为指定格式字符串
this.registerModule(simpleModule);
}
}
配置
import com.itcast.interceptor.LogInterceptor;
import com.itcast.utils.JacksonObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
/** * @author : Maxim * @date : 2022-05-29 22:56 * @description : **/
@Configuration
public class RegisConfig implements WebMvcConfigurer {
//配置全局类型转换器
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建消息转换器对象
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
//设置对象转换器 底层使用Jackson将Java对象转为Json
mappingJackson2HttpMessageConverter.setObjectMapper(new JacksonObjectMapper());
//将上面的信息转换器对象追加到mvc框架的转换器集合
converters.add(0, mappingJackson2HttpMessageConverter);
}
}
边栏推荐
猜你喜欢

Classes and interfaces

文本编辑器vim

【Image Classification】2022-CycleMLP ICLR

过水滑环的结构和工作原理

matlab simulink response spectrum calculation

WPF 实现更换主题色

黑马jvm课程笔记d2

js原型和原型链以及原型继承

Difference between netstat and ss command

Evaluation and Construction of Enterprise Network Security Capability from the Sliding Ruler Model
随机推荐
互联网公司高频面试题精讲:测试计划和测试方案有什么区别?
软件测试这些基本类型你知道吗?
所谓软件测试工作能力强,其实就是这 5 点
从8k到13k,我全靠这本《接口自动化测试——从入门到精通》
《天才基本法》:平行时空的第二次选择,小演员的表现意外出圈
电话自动拨号在电脑上自动拨打
关于redis在业务中的应用问题,如何解决?
Classes and interfaces
【Image Classification】2022-CycleMLP ICLR
测试工作管理与规范
goland控制台显示重叠问题解决方案
Pen paper records
fastjson autoType is not support
金融口译,口译中金融高频词有哪些
黑马jvm课程笔记d2
uniapp 路由与页面跳转
【每日一题】大佬们进来看看吧
vue项目 npm run build 打包项目防止浏览器缓存
WPF 实现更换主题色
如何整合全流程数据,全面提升研发效能?|2分钟了解 ONES