<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
controller层
@RestController
public class JsonController {
/**
* 测试使用@Bean注解方式实现全局序列化配置
*
* @return
*/
@GetMapping("/testJacksonByBean")
public List<Object> testJacksonByBean() {
List<Object> list = new ArrayList<>();
list.add(1440931124753108994L);
list.add(new Date());
return list;
}
}
没有配置之前测试结果:
[
1440931124753108994,
"2022-01-21T03:44:38.365+0000"
]
全局序列化配置类:
/**
* 全局序列化配置类
*/
@Configuration
public class JsonConfig {
/**
* 创建Jackson对象映射器
*
* @param builder Jackson对象映射器构建器
* @return ObjectMapper
*/
@Bean
public ObjectMapper getJacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
//序列换成json时,将所有的long变成string.因为js中得数字类型不能包含所有的java long值,超过16位后会出现精度丢失
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
//反序列化的时候如果多了其他属性,不抛出异常
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//日期格式处理
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
return objectMapper;
}
}
测试结果:
[
"1440931124753108994",
"2022-01-21 11:50:08"
]
全局序列化配置类:
/**
* 全局序列化配置类
*/
@Configuration
@EnableWebMvc
public class JsonConfig implements WebMvcConfigurer {
/**
* 全局序列化方式
*
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//Jackson的全局序列化方式
configureJacksonHttpMessageConverter(converters);
}
/**
* Jackson的全局序列化方式
*
* @param converters
*/
private void configureJacksonHttpMessageConverter(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
//序列换成json时,将所有的long变成string.因为js中得数字类型不能包含所有的java long值,超过16位后会出现精度丢失
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(Long.class, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
simpleModule.addSerializer(Long.TYPE, com.fasterxml.jackson.databind.ser.std.ToStringSerializer.instance);
objectMapper.registerModule(simpleModule);
//反序列化的时候如果多了其他属性,不抛出异常
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//日期格式处理
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
converter.setObjectMapper(objectMapper);
converters.add(converter);
converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
}
}
测试结果:
[
"1440931124753108994",
"2022-01-21 11:52:45"
]
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_43296313/article/details/122618360
内容来源于网络,如有侵权,请联系作者删除!