自定义全局jackson序列化

描述在WEB开发中前后台使用JSON传输,难免前后台格式的转换的问题。以下是以SPRINGMVC开发为例说明

如:日期的转换;null的转换等

 

1:自定义一个继承ObjectMapper的类

/**
 * 自定义的JSON转换MAPPER
 * @author xixi
 * @date 2013-6-24
 *
 */
public class CustomObjectMapper extends ObjectMapper {

	public CustomObjectMapper(){
		super();
		//设置null转换""
		getSerializerProvider().setNullValueSerializer(new NullSerializer());
		//设置日期转换yyyy-MM-dd HH:mm:ss
		setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
	}
	
	//null的JSON序列
	private class NullSerializer extends JsonSerializer<Object> {
		public void serialize(Object value, JsonGenerator jgen,
				SerializerProvider provider) throws IOException,
				JsonProcessingException {
			jgen.writeString("");
		}
	}
}

 2:在spring的配置文件中定义这个自定义的Mapper

	<!-- 启动 MVC注解 -->
	<mvc:annotation-driven>
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
				<property name="objectMapper" ref="customObjectMapper" />
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

	<!-- 自定义的JSON ObjectMapper -->
	<bean id="customObjectMapper" class="com.xixi.web4j.util.CustomObjectMapper" />

 就这么简单,不用再像每个POJO对像上去@JsonSerializer(using...)了。

以后对所有DATE类型字段序列化JSON,都会默认传为“yyyy-MM-dd HH:mm:ss”格式,对于字段为null的也会转为“”。如果你对null有别的处理。你可以在自定义的NullSerializer修改你想要的格式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 LocalDateTime 序列化序列化全局配置类示例: ```java import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; import java.time.LocalDateTime; @Configuration public class LocalDateTimeConfig { @Bean public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); objectMapper.registerModule(javaTimeModule); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); return objectMapper; } } ``` 在上面的代码中,我们首先创建了一个 `ObjectMapper` 对象,并添加了 `JavaTimeModule` 模块,该模块包含了序列化和反序列化 `LocalDateTime` 对象的方法。然后我们将此 `ObjectMapper` 注册到 Spring 的全局配置中,并关闭了时间戳的序列化。 接下来我们需要自定义 `LocalDateTimeSerializer` 和 `LocalDateTimeDeserializer` 类来实现 `LocalDateTime` 的序列化和反序列化: ```java import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(formatter.format(value)); } } ``` ```java import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { private static final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { return LocalDateTime.parse(p.getValueAsString(), formatter); } } ``` 现在,我们就可以使用 `LocalDateTime` 类型的属性在 RESTful API 中进行序列化和反序列化了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值