Spring boot jackson 时间格式问题

这个问题是具体表现是这样的:

Spring boot Application  直接使用IDEA 运行没有任何问题,使用maven 打包也不存在问题,但是在打包之后执行时一直提示出现错误。错误的信息大致如下:

1Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled. 22017-06-30 23:36:35.931 ERROR 11916 --- [ main] o.s.boot.SpringApplication : Application startup failed 3 4org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonObjectMapperBuilder' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonA 5utoConfiguration$JacksonObjectMapperBuilderConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instanti 6ate [org.springframework.http.converter.json.Jackson2ObjectMapperBuilder]: Factory method 'jacksonObjectMapperBuilder' threw exception; nested exception is java.lang.IllegalArgumentException: name 7 at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.9.RELEASE.jar!/:4.3.9.RELEASE]

错误信息很多,干扰很多,不过最重要的是 jackson 的问题。

引起这个问题的原因是:

spring boot 往网页上输出了一个包含Date 的jason 字符串。

这个Date 转化为jason 的时间格式写在 application.yml 文件中:

1spring: 2 jackson: 3 date-format: HH:mm:ss.SSSSSS 4 joda-date-time-format: HH:mm:ss.SSSSSS
  • 删除application.yml 中的这几行,这个问题就不存在了。

删除之后, mvn clean package 打包不存在问,打包之后也能够正常输出, 但是会将时间字符串变成时间戳!

正常的是这样:

1{ 2"id": 42, 3"curingTime": "15:35:55.000370" 4}

删除之后变成了这样:

1{ 2"id": 43, 3"curingTime": 56720161 4}
  • 使用 Gson 解决这个问题

这种情况下,我暂时不知道怎么解决。不过我通过不断查找,发现一个使用Gson 替代的方案:

首先,排除所有jackson 依赖!

              pom.xml 中更改如下:

1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-web</artifactId> 4 <exclusions> 5 <!--排除jackson 依赖--> 6 <exclusion> 7 <groupId>com.fasterxml.jackson.core</groupId> 8 <artifactId>jackson-databind</artifactId> 9 </exclusion> 10 </exclusions> 11 </dependency> 12 <!--添加gson--> 13 <dependency> 14 <groupId>com.google.code.gson</groupId> 15 <artifactId>gson</artifactId> 16 </dependency>

因为Spring boot 在不存在jackson 的时候会自动查找gson, 因此这样就可以直接用了。

然后, 更改jpa 表中的Date 项(如果有的话,因为jackson 都不存在了,所有依赖他的都必须要改):

1@Temporal(TemporalType.TIME) 2@DateTimeFormat(pattern = "HH:mm:ss.ssssss") 3//@JsonFormat(shape = JsonFormat.Shape.STRING,pattern = "HH:mm:ss.SSSSSS",timezone = "GMT+8") 4private Date curingTime;

然后,重新在application.yml 文件中添加上面被删掉的代码!

此时运行程序,得到的结果多半是这样的:

1{ 2"id": 44, 3"curingTime": "Jan 1, 1970 11:55:00 PM" 4}

还是与目标不符合,但是已经接近目标了!

最后一步:启动类中添加Gson Bean, 如下:

1public static void main(String[] args) { 2 SpringApplication.run(DemoApplication.class, args); 3} 4 5@Bean 6public Gson getGson() { 7 Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss.SSSSSS").create(); 8 return gson; 9}

Spring boot 会自动加载这个Bean,达到了最终目的!

其实,加上Gson 之后,application.yml 中的那三行代码就可以不用要了。

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

vue-element-admin项目打包后,iconfont图标出现乱码

使用vueelementadmin或者vueelementtemplate开发的项目,打包到线上,就出现了图标乱码,f12后能看到icon元素为.eliconclose:before{content:"□"}的情况(如下)

Nginx 报404问题,如何解决

近日在使用服务器部署项目时,出现了一些问题,如图正常的登录界面是可以访问的,但是在登录之后访问之后的地址会报404错误,于是去查看是否配置有错误,但是查看之后发现,nginx.conf与config.js两个配置文件的ip和端口都是没有错误的这个项目部署过好多次,没有出现过这样的错误。这是原版没动过的解压缩后的nginx.conf的源文件圈起来的地方是应该按

Spring Boot:jar中没有主清单属性

使用SpringBoot微服务搭建框架时,使用IDEA可以正常运行,但是使用MAVEN打包工具打包成jar后运行时,提示错误:未找到主清单目录。查看pom文件,发现已添加SpringBoot的构建插件xmlorg.springframework.bootspringbootmavenplugin2.4.1

PHP扩展开发小记

  之前开发的Xukey,在PHP5.6和5.6之前的版本都没有任何问题,在兼容PHP7之后,执行生成随机数会出现“”段错误“”。编译扩展时加入debug,提示phpterminated\\\stacksmashingdete。这样的错误和没有提示直接退出没有什么区别。之后又用gdb调试,这个错误明显一些,提示 /sysdeps/x86\

Maven打包的三种方式(转)

Maven可以使用mvnpackage指令对项目进行打包,如果使用Java jarxxx.jar执行运行jar文件,会出现"nomainmanifestattribute,inxxx.jar"(没有设置MainClass)、ClassNotFoundException(找不到依赖包)等错误。要想jar包能直接通过javajarxx

Spring boot jackson 时间格式问题 - HelloWorld