http://wujiu.iteye.com/blog/2244537
我们在使用Spring MVC 是都有这样的经历,@ResponseBody返回一个JSON格式字符串到前端,或者使用@RequestBody获取前端REST提交的对象,那么Spring MVC 中是如何处理的呢?大体的流程如下:
在Spring Web中使用 jackson处理,具体实现类是MappingJackson2HttpMessageConverter. 具体的JSON系列化和反序列化都是有jackson实现的。下面主要介绍一下jackson的使用。
对日期进行JSON序列化:
Java代码
/**
*
* @author zhangwei
* @since 2015年9月18日 下午4:08:08
* @version v 0.1
*/
publicclassContainDateObject {
privateDate startTime;
privateDate endTime;
publicDate getStartTime() {
returnstartTime;
}
publicvoidsetStartTime(Date startTime) {
this.startTime = startTime;
}
publicDate getEndTime() {
returnendTime;
}
publicvoidsetEndTime(Date endTime) {
this.endTime = endTime;
}
}
Java代码
@Test
publicvoidtestDate()throwsJsonProcessingException {
ContainDateObject cdo =newContainDateObject();
cdo.setStartTime(newDate());
cdo.setEndTime(newDate());
System.out.println(objectMapper.writeValueAsString(cdo));
}
结果是:
Java代码
{"startTime":1442563815734,"endTime":1442563815734}
这样的结果有的时候是无法接受的,我们有的时候需要一个yyyy-MM-dd HH:mm:ss这样格式的字符串改如何处理呢?这个时候我们就需要制定一个自定义的序列化类;
Java代码
/**
* Desc:自定义日期类型序列化类
*
* @author zhangwei
* @since 2015年9月18日 下午4:12:51
* @version v 0.1
*/
publicclassDateJsonSerializerextendsJsonSerializer {
/**
* @see com.fasterxml.jackson.databind.JsonSerializer#serialize(java.lang.Object,
* com.fasterxml.jackson.core.JsonGenerator,
* com.fasterxml.jackson.databind.SerializerProvider)
*/
@Override
publicvoidserialize(Date date, JsonGenerator generator, SerializerProvider provider)
throwsIOException, JsonProcessingException {
ObjectMapper om =newObjectMapper();
SimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
om.writeValue(generator, sdf.format(date));
}
}
在需要按照指定格式进行序列化的日期属性上添加注解
Java代码
@JsonSerialize(using = DateJsonSerializer.class)
privateDate startTime;
@JsonSerialize(using = DateJsonSerializer.class)
privateDate endTime;
测试的结果是:
Java代码
{"startTime":"2015-09-18 16:23:23","endTime":"2015-09-18 16:23:23"}
这样的结果就比较符合预期了
反过来,使用这个JSON转换为一个对象时的结果怎样呢?
Java代码
@Test
publicvoidtestDate()throwsException {
ContainDateObject cdo =newContainDateObject();
cdo.setStartTime(newDate());
cdo.setEndTime(newDate());
String serializerResult = objectMapper.writeValueAsString(cdo);
cdo = objectMapper.readValue(serializerResult, ContainDateObject.class);
}
结果却是:
Java代码
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)
反序列化失败了,这又改如何处理呢?我们同样指定反序列化类
Java代码
/**
* Desc:日期类型的反序列化类
*
* @author zhangwei
* @since 2015年9月18日 下午4:31:02
* @version v 0.1
*/
publicclassDateJsonDeserializerextendsJsonDeserializer {
/**
* @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser,
* com.fasterxml.jackson.databind.DeserializationContext)
*/
@Override
publicDate deserialize(JsonParser parser, DeserializationContext context)
throwsIOException, JsonProcessingException {
try{
SimpleDateFormat sdf =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
returnsdf.parse(parser.getValueAsString());
}catch(Exception e) {
e.printStackTrace();
}
returnnull;
}
}
Java代码
@JsonSerialize(using = DateJsonSerializer.class)
@JsonDeserialize(using = DateJsonDeserializer.class)
privateDate startTime;
@JsonSerialize(using = DateJsonSerializer.class)
@JsonDeserialize(using = DateJsonDeserializer.class)
privateDate endTime;
这样反序列化问题也可以很好地解决了。
我们再想对象中添加一个个人隐私信息字段 phone;
Java代码
@JsonSerialize(using = DateJsonSerializer.class)
@JsonDeserialize(using = DateJsonDeserializer.class)
privateDate startTime;
@JsonSerialize(using = DateJsonSerializer.class)
@JsonDeserialize(using = DateJsonDeserializer.class)
privateDate endTime;
privateString phone;
Java代码
@Test
publicvoidtestDate()throwsException {
ContainDateObject cdo =newContainDateObject();
cdo.setStartTime(newDate());
cdo.setEndTime(newDate());
cdo.setPhone("13919309243");
String serializerResult = objectMapper.writeValueAsString(cdo);
System.out.println(serializerResult);
objectMapper.readValue(serializerResult, ContainDateObject.class);
}
这样序列化的结果是:
Java代码
{"startTime":"2015-09-18 16:37:34","endTime":"2015-09-18 16:37:34","phone":"13919309243"}
phone是一个个人隐私信息,如果不想被序列化改如何处理?只需要在该属性上添加一个注解
Java代码
@JsonIgnore
privateString phone;
再次序列化的结果是:
Java代码
{"startTime":"2015-09-18 16:43:15","endTime":"2015-09-18 16:43:15"}