怎么做文化传媒公司网站,后端开发百度百科,好看大方的企业网站源码.net,网站频道策划*本例程序使用Jackson2.9.0#xff0c;jackson1.x的处理方式稍稍有些不同。 在基于SpringSpringMVC的Web项目中#xff0c;我们常使用Jackson(1.x/2.x)来增加程序对Json格式的数据的支持。 因此#xff0c;在实际应用中有个常见的需求#xff1a;日期的格式化。 假设jackson1.x的处理方式稍稍有些不同。 在基于SpringSpringMVC的Web项目中我们常使用Jackson(1.x/2.x)来增加程序对Json格式的数据的支持。 因此在实际应用中有个常见的需求日期的格式化。 假设User对象有个Date类型的属性birthday class User implements Serializable {private Date birthday;//...
} 程序支持如下api请求而我们希望在返回Json格式的User资料时对Date类型的birthday进行一下格式化。 Controller
class UserAction {RequestMapping(/user/find/{id})public ResponseBody User getUserById(PathVariable(id) int id) {return userService.getUserById(id);}
} 实现上述需求大体有两种常用的方式 1.使用JsonFormat注解 该方法只需在关键字段加上JsonFormat注解即可如下 class User implements Serializable {JsonFormat(pattern yyyy-MM-dd HH:mm:ss, timezone GMT8)private Date birthday;//...
} 参数解释pattern - 格式timezone - 时区 2.设置MappingJackson2HttpMessageConverter的objectMapper 该方法主要对json数据转换时用到的HttpMessageConverter进行一些设置进一步讲就是objectMapper在对日期类型数据序列化时设置成统一的pattern配置如下 mvc:annotation-drivenmvc:message-convertersbean classorg.springframework.http.converter.json.MappingJackson2HttpMessageConverterproperty nameobjectMapperbean classcom.fasterxml.jackson.databind.ObjectMapperproperty namedateFormatbean classjava.text.SimpleDateFormatconstructor-arg typejava.lang.String valueyyyy-MM-dd HH:mm:ss //bean/property/bean/property/bean/mvc:message-converters
/mvc:annotation-driven 或者 bean idobjectMapper classorg.springframework.http.converter.json.Jackson2ObjectMapperFactoryBeanproperty namesimpleDateFormat valueyyyy-MM-dd HH:mm:ss /
/beanmvc:annotation-drivenmvc:message-convertersbean classorg.springframework.http.converter.json.MappingJackson2HttpMessageConverterproperty nameobjectMapper refobjectMapper //bean/mvc:message-converters
/mvc:annotation-driven 方法1使用起来方便灵活但如果有大量需要统一设置的字段属性那么推荐使用方法2。或者两种方法混合使用作用优先级方法1 方法2。转载于:https://www.cnblogs.com/lichmama/p/7867273.html