Spring boot jackson 时间格式问题

Stella981
• 阅读 596

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

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

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-30 23:36:35.931 ERROR 11916 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonObjectMapperBuilder' defined in class path resource [org/springframework/boot/autoconfigure/jackson/JacksonA
utoConfiguration$JacksonObjectMapperBuilderConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instanti
ate [org.springframework.http.converter.json.Jackson2ObjectMapperBuilder]: Factory method 'jacksonObjectMapperBuilder' threw exception; nested exception is java.lang.IllegalArgumentException: name
        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 文件中:

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

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

正常的是这样:

{
"id": 42,
"curingTime": "15:35:55.000370"
}

删除之后变成了这样:

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

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

首先,排除所有jackson 依赖!

              pom.xml 中更改如下:

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                 <!--排除jackson 依赖-->
                <exclusion>
                    <groupId>com.fasterxml.jackson.core</groupId>
                    <artifactId>jackson-databind</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--添加gson-->
       <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>

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

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

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

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

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

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

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

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

public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}

@Bean
public Gson getGson() {
    Gson gson = new GsonBuilder().setDateFormat("HH:mm:ss.SSSSSS").create();
    return gson;
}

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

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

点赞
收藏
评论区
推荐文章
橘子橙 橘子橙
3年前
vue-element-admin项目打包后,iconfont图标出现乱码
使用vueelementadmin或者vueelementtemplate开发的项目,打包到线上,就出现了图标乱码,f12后能看到icon元素为.eliconclose:before{content:"□"}的情况(如下)
芝士年糕 芝士年糕
1年前
Nginx 报404问题,如何解决
近日在使用服务器部署项目时,出现了一些问题,如图正常的登录界面是可以访问的,但是在登录之后访问之后的地址会报404错误,于是去查看是否配置有错误,但是查看之后发现,nginx.conf与config.js两个配置文件的ip和端口都是没有错误的这个项目部署过好多次,没有出现过这样的错误。这是原版没动过的解压缩后的nginx.conf的源文件圈起来的地方是应该按
红烧土豆泥 红烧土豆泥
2年前
Spring Boot:jar中没有主清单属性
使用SpringBoot微服务搭建框架时,使用IDEA可以正常运行,但是使用MAVEN打包工具打包成jar后运行时,提示错误:未找到主清单目录。查看pom文件,发现已添加SpringBoot的构建插件xmlorg.springframework.bootspringbootmavenplugin2.4.1
Wesley13 Wesley13
2年前
Failed to get nested archive for entry BOOT
环境:springbootkafkaES。。。。。。maven3.6jdk8问题是jar包启动失败报错,错误如下:Exceptioninthread"main"java.lang.IllegalStateException:FailedtogetnestedarchiveforentryBO
Wesley13 Wesley13
2年前
PHP扩展开发小记
  之前开发的Xukey,在PHP5.6和5.6之前的版本都没有任何问题,在兼容PHP7之后,执行生成随机数会出现“”段错误“”。编译扩展时加入debug,提示phpterminated\\\stacksmashingdete。这样的错误和没有提示直接退出没有什么区别。之后又用gdb调试,这个错误明显一些,提示 /sysdeps/x86\
Stella981 Stella981
2年前
Maven打包的三种方式(转)
Maven可以使用mvnpackage指令对项目进行打包,如果使用Java jarxxx.jar执行运行jar文件,会出现"nomainmanifestattribute,inxxx.jar"(没有设置MainClass)、ClassNotFoundException(找不到依赖包)等错误。要想jar包能直接通过javajarxx
Stella981 Stella981
2年前
IntelliJ IDEA 使用JSTL标签库(IDEA 加载 tld文件)
这两天在 IntelliJIDEA中 使用C标签(JSTL)的时候发现引入进来有问题,不但没提示而且还有红色警告,所以经过一番谷歌的查找之后解决了这个问题,发现是没有导入tld文件的问题。在一开始的时候你在IDEA里面使用C标签或者JSTL的其他2个标签库时都会有红色警告的而且没有提示,如下图!(http://static.oschina
Stella981 Stella981
2年前
IntelliJ Idea解决Could not autowire
IntelliJIdea解决Couldnotautowire.Nobeansof'xxxx'typefound的错误提示1.问题描述  在Idea的spring工程里,经常会遇到
Stella981 Stella981
2年前
IntelliJ Idea取消Could not autowire. No beans of 'xxxx' type found的错误提示
1.问题描述在Idea的spring工程里,经常会遇到Couldnotautowire.Nobeansof'xxxx'typefound的错误提示。但程序的编译和运行都是没有问题的,这个错误提示并不会产生影响。但红色的错误提示在有些有强迫症的程序员眼里,多多少少有些不太舒服。!输入图片说明(https://static.o
Stella981 Stella981
2年前
Mapped Statements collection does not contain
这两天测试在测试代码,采用的ibatis,一直报标题的错误。看了一下代码发现错误是调用的xml的ID名称和xml中的ID名称对不上。网上找了一下还有可能出现这个问题的原因,总结如下: 1、mapper.xml中没有加入namespace 2、mapper.xml中的方法和接口mapper的方法不对应 3、mapper.xml没有加入到my