Spring fastjson format 无效
2017-11-01 22:20
309 查看
最近在前端看到时间的格式不对,然后一顿调试,配置了fastjson和spring的整合,给实体类添加了注解,但是都没有生效,整整一天半
后才找到原因
版本
spring:4.1.4
fastjson:1.2.7
有以下两种配置方式
这种方法可能并没有生效,则可以尝试在getter、setter方法上添加注解
最后的重点就是spring-mvc.xml中,配置的json转换器必须在这段扫描包的配置之前,要不然转换器可能无法生效,这就是我找了一天半的bug
后才找到原因
版本
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:请看准版本,欢迎各位指出不足,补充不同的解决方案
相关文章推荐
- SpringBoot(十):SpringBoot使用FastJson
- Springboot 添加fastjson依赖包
- SpringBoot使用FastJson 替换 jackson
- spring-boot实现FastJson解析json数据
- spring boot 配置fastjson解析json
- 解决Springboot使用FastJson 返回中文乱码的问题
- spring4.2+mybatis3.3+freemark+log4j2+fastjson整合
- SpringMVC+FastJson+Swagger集成完整示例
- Android studio利用GsonFormat和fastjson快速解析JSON!
- Spring MVC + fastjson实现异步加载
- SpringBoot学习-(2)使用FastJson作为json解析器
- Spring-boot中使用fastjson作为json框架
- SpringMVC+fastjson项目配置
- Spring MVC+FastJson+hibernate-validator整合的完整实例教程
- (4)Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
- Springboot使用FastJsonHttpMessageConverter导致Swagger2失效
- Spring Boot 使用第三方json解析 : fastjson
- fastjson在springboot中的使用
- Spring Boot 使用json框架fastjson