十四、自定义Jackson ObjectMapper把Long型转化为String类型

来源:互联网 发布:2016网络效应试题答案 编辑:程序博客网 时间:2024/05/21 21:43

在一个HTTP交互中,Spring MVC(客户端和服务端)使用HttpMessageConverters协商内容转换。如果classpath下存在Jackson,你就已经获取到Jackson2ObjectMapperBuilder提供的默认转换器。

创建的ObjectMapper(或用于Jackson XML转换的XmlMapper)实例默认有以下自定义属性:

  • MapperFeature.DEFAULT_VIEW_INCLUSION禁用
  • DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES禁用

    Spring Boot也有一些简化自定义该行为的特性。

你可以使用当前的environment配置ObjectMapper和XmlMapper实例。Jackson提供一个扩展套件,可以用来简单的关闭或开启一些特性,你可以用它们配置Jackson处理的不同方面。这些特性在Jackson中使用5个枚举进行描述的,并被映射到environment的属性上:

Jackson枚举 Environment属性 com.fasterxml.jackson.databind.DeserializationFeature `spring.jackson.deserialization.=true false` com.fasterxml.jackson.core.JsonGenerator.Feature `spring.jackson.generator.=true false` com.fasterxml.jackson.databind.MapperFeature `spring.jackson.mapper.=true false` com.fasterxml.jackson.core.JsonParser.Feature `spring.jackson.parser.=true false` com.fasterxml.jackson.databind.SerializationFeature `spring.jackson.serialization.=true false

例如,设置spring.jackson.serialization.indent_output=true可以开启漂亮打印。注意,由于松绑定的使用,indent_output不必匹配对应的枚举常量INDENT_OUTPUT。

如果想彻底替换默认的ObjectMapper,你需要定义一个该类型的@Bean并将它标记为@Primary。

定义一个Jackson2ObjectMapperBuilder类型的@Bean将允许你自定义默认的ObjectMapper和XmlMapper(分别用于MappingJackson2HttpMessageConverter和MappingJackson2XmlHttpMessageConverter)。

另一种自定义Jackson的方法是向你的上下文添加com.fasterxml.jackson.databind.Module类型的beans。它们会被注册入每个ObjectMapper类型的bean,当为你的应用添加新特性时,这就提供了一种全局机制来贡献自定义模块。

最后,如果你提供任何MappingJackson2HttpMessageConverter类型的@Beans,那它们将替换MVC配置中的默认值。同时,也提供一个HttpMessageConverters类型的bean,它有一些有用的方法可以获取默认的和用户增强的message转换器。

实现一个自定义的Jackson ObjectMapper

在调用@RequestBody的时候把Long型转化为String类型

package com.hd.oms.config;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.module.SimpleModule;import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;import org.springframework.context.annotation.Configuration;import org.springframework.http.converter.HttpMessageConverter;import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import java.util.List;/** * Created by LF on 2017/4/5. */@Configurationpublic class LongToString extends WebMvcConfigurerAdapter {    @Override    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();        //不显示为null的字段        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);        SimpleModule simpleModule = new SimpleModule();        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);        objectMapper.registerModule(simpleModule);        jackson2HttpMessageConverter.setObjectMapper(objectMapper);        //放到第一个        converters.add(0, jackson2HttpMessageConverter);    }}
0 0
原创粉丝点击