我们在使用Spring MVC 是都有这样的经历,@ResponseBody返回一个JSON格式字符串到前端,或者使用@RequestBody获取前端REST提交的对象,那么Spring MVC 中是如何处理的呢?大体的流程如下:
在Spring Web中使用 jackson处理,具体实现类是MappingJackson2HttpMessageConverter. 具体的JSON系列化和反序列化都是有jackson实现的。下面主要介绍一下jackson的使用。
对日期进行JSON序列化:
/** * * @author zhangwei<wei.zw@corp.netease.com> * @since 2015年9月18日 下午4:08:08 * @version v 0.1 */ public class ContainDateObject { private Date startTime; private Date endTime; public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } }
@Test public void testDate() throws JsonProcessingException { ContainDateObject cdo = new ContainDateObject(); cdo.setStartTime(new Date()); cdo.setEndTime(new Date()); System.out.println(objectMapper.writeValueAsString(cdo)); }
结果是:
{"startTime":1442563815734,"endTime":1442563815734}
这样的结果有的时候是无法接受的,我们有的时候需要一个yyyy-MM-dd HH:mm:ss这样格式的字符串改如何处理呢?这个时候我们就需要制定一个自定义的序列化类;
/** * Desc:自定义日期类型序列化类 * * @author zhangwei<wei.zw@corp.netease.com> * @since 2015年9月18日 下午4:12:51 * @version v 0.1 */ public class DateJsonSerializer extends JsonSerializer<Date> { /** * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object, * com.fasterxml.jackson.core.JsonGenerator, * com.fasterxml.jackson.databind.SerializerProvider) */ @Override public void serialize(Date date, JsonGenerator generator, SerializerProvider provider) throws IOException, JsonProcessingException { ObjectMapper om = new ObjectMapper(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); om.writeValue(generator, sdf.format(date)); } }
在需要按照指定格式进行序列化的日期属性上添加注解
@JsonSerialize(using = DateJsonSerializer.class) private Date startTime; @JsonSerialize(using = DateJsonSerializer.class) private Date endTime;
测试的结果是:
{"startTime":"2015-09-18 16:23:23","endTime":"2015-09-18 16:23:23"}
这样的结果就比较符合预期了
反过来,使用这个JSON转换为一个对象时的结果怎样呢?
@Test public void testDate() throws Exception { ContainDateObject cdo = new ContainDateObject(); cdo.setStartTime(new Date()); cdo.setEndTime(new Date()); String serializerResult = objectMapper.writeValueAsString(cdo); cdo = objectMapper.readValue(serializerResult, ContainDateObject.class); }
结果却是:
com.fasterxml.jackson.databind.exc.InvalidFormatException: Can not construct instance of java.util.Date from String value '2015-09-18 16:26:58': not a valid representation (error: Failed to parse Date value '2015-09-18 16:26:58': Can not parse date "2015-09-18 16:26:58": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")) at [Source: {"startTime":"2015-09-18 16:26:58","endTime":"2015-09-18 16:26:58"}; line: 1, column: 2] (through reference chain: com.david.demo.jackson.ContainDateObject["startTime"]) at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:55) at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:883) at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseDate(StdDeserializer.java:750) at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateBasedDeserializer._parseDate(DateDeserializers.java:176) at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:262) at com.fasterxml.jackson.databind.deser.std.DateDeserializers$DateDeserializer.deserialize(DateDeserializers.java:246) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523) at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95) at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285) at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:248) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:136) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2578)
反序列化失败了,这又改如何处理呢?我们同样指定反序列化类
/** * Desc:日期类型的反序列化类 * * @author zhangwei<wei.zw@corp.netease.com> * @since 2015年9月18日 下午4:31:02 * @version v 0.1 */ public class DateJsonDeserializer extends JsonDeserializer<Date> { /** * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, * com.fasterxml.jackson.databind.DeserializationContext) */ @Override public Date deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.parse(parser.getValueAsString()); } catch (Exception e) { e.printStackTrace(); } return null; } }
@JsonSerialize(using = DateJsonSerializer.class) @JsonDeserialize(using = DateJsonDeserializer.class) private Date startTime; @JsonSerialize(using = DateJsonSerializer.class) @JsonDeserialize(using = DateJsonDeserializer.class) private Date endTime;
这样反序列化问题也可以很好地解决了。
我们再想对象中添加一个个人隐私信息字段 phone;
@JsonSerialize(using = DateJsonSerializer.class) @JsonDeserialize(using = DateJsonDeserializer.class) private Date startTime; @JsonSerialize(using = DateJsonSerializer.class) @JsonDeserialize(using = DateJsonDeserializer.class) private Date endTime; private String phone;
@Test public void testDate() throws Exception { ContainDateObject cdo = new ContainDateObject(); cdo.setStartTime(new Date()); cdo.setEndTime(new Date()); cdo.setPhone("13919309243"); String serializerResult = objectMapper.writeValueAsString(cdo); System.out.println(serializerResult); objectMapper.readValue(serializerResult, ContainDateObject.class); }
这样序列化的结果是:
{"startTime":"2015-09-18 16:37:34","endTime":"2015-09-18 16:37:34","phone":"13919309243"}
phone是一个个人隐私信息,如果不想被序列化改如何处理?只需要在该属性上添加一个注解
@JsonIgnore private String phone;
再次序列化的结果是:
{"startTime":"2015-09-18 16:43:15","endTime":"2015-09-18 16:43:15"}