Spring fastjson format 无效
来源:互联网 发布:如何考上清华知乎 编辑:程序博客网 时间:2024/04/29 10:18
最近在前端看到时间的格式不对,然后一顿调试,配置了fastjson和spring的整合,给实体类添加了注解,但是都没有生效,整整一天半后才找到原因
版本
spring:4.1.4
fastjson:1.2.7
有以下两种配置方式
1.在spring-mvc.xml中配置Date的日期转换器
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- 配置Fastjson支持 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <!-- 输出key时是否使用双引号 --> <value>QuoteFieldNames</value> <!-- 是否输出值为null的字段 --> <value>WriteMapNullValue</value> <!-- List字段如果为null,输出为[],而非null --> <value>WriteNullListAsEmpty</value> <!-- 字符类型字段如果为null,输出为”“,而非null --> <value>WriteNullStringAsEmpty</value> <!-- 字符类型字段如果为null,输出为”“,而非null --> <value>WriteNullNumberAsZero</value> <!-- Date的日期转换器 --> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
2.在实体属性上配置注解
@JSONField(format = "yyyy-MM-dd HH:mm:ss")private Date login_time;//上次登陆时间
这种方法可能并没有生效,则可以尝试在getter、setter方法上添加注解
@JSONField(format = "yyyy-MM-dd HH:mm:ss")public Date getLogin_time() {return login_time;}@JSONField(format = "yyyy-MM-dd HH:mm:ss")public void setLogin_time(Date login_time) {this.login_time = login_time;}
最后的重点就是spring-mvc.xml中,配置的json转换器必须在这段扫描包的配置之前,要不然转换器可能无法生效,这就是我找了一天半的bug
<context:component-scan base-package=""></context:component-scan>PS:请看准版本,欢迎各位指出不足,补充不同的解决方案
阅读全文
0 0
- Spring fastjson format 无效
- fastjson date format
- Spring MVC整合fastjson
- spring mvc3 + fastjson
- spring 下配置 fastJson
- spring boot 配置fastjson
- spring配置fastjson解析
- spring boot 使用fastjson
- spring boot使用fastjson
- spring boot + fastjson
- Spring MVC 4 - 整合fastjson
- spring-mvc中配置fastjson
- spring MVC-fastjson中文乱码
- spring boot杂记fastjson swagger
- spring boot 整合 resteasy+fastjson
- Spring boot引入引入fastjson
- spring boot 二 集成 FastJson
- hibernate+spring,无效情况
- 微信公众平台开发
- 分享一个硬件SPI读取TFT触摸屏程序
- mySql数据库出问题,整理一下mySql的安装与配置
- mybatis中的延迟加载
- hook read_chk 导致dex2oat进程 abort
- Spring fastjson format 无效
- POI解析文档内容(txt,doc,docx,xls,xlsx,ppt,pdf)
- LeetCode——Merge Two Sorted Lists
- 007 PyQuery
- Android 自定义布局对话框
- git创建仓库
- Java8函数式编程之三:函数式接口
- 用R进行文本挖掘与分析--软件分词统计词频
- 【模板】二叉搜索树