美文网首页
springBoot使用json处理Date的几种方法(环境为2

springBoot使用json处理Date的几种方法(环境为2

作者: 程序员小杰 | 来源:发表于2020-01-09 16:25 被阅读0次

1、创建实体类User,添加属性跟get、set方法。

private String id;
private String username;
private Date createTime;

2、创建UserController,编写控制层代码

@GetMapping("/getUser")
    public List<User> getUser() {
        List<User> userList = new ArrayList<User>();
        for (int i=1; i<=5; i++) {
            User user = new User();
            user.setCreateTime(new Date());
            user.setUsername("gongj" + i);
            user.setId("j" + i);
            userList.add(user);
        }
        return userList;
    }

调用接口:http://localhost:8080/getUser

image.png

该结果显然不是我们需要的,所以我们需要格式化一下。

第一种 使用注解:

private String id;
private String username;
@JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
private Date createTime;

pattern:是你需要转换的时间日期的格式
timezone:是时间设置为东八区,避免时间在转换中有误差

调用接口:http://localhost:8080/getUser

image.png

完成,但是这种也有不好的地方,如果我有一百个实体中都有Date类型,那就要在一百个实体加入注解。

第二种 修改默认配置

所有的json生成都离不开相关的HttpMessageConverters

spring boot 默认使用的jackson,并对默认做了配置。所以我们来修改一下。

ctrl+shift+h 搜索 JacksonHttpMessageConvertersConfiguration


image.png

该类中这个方法就是用来处理json的。


image.png
那怎么做呢!springBoot是如果你没提供,就使用springBoot默认提供的,你提供了,springBoot就使用你的。

新建MvcConfig

import java.text.SimpleDateFormat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
public class MvcConfig {

    @Bean
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverterConfiguration() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper om = new ObjectMapper();
        //全局修改josn时间格式
        om.setDateFormat(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
        converter.setObjectMapper(om);
        return converter;
    }

我们提供了一个MappingJackson2HttpMessageConverter ,所以springboot就会使用我们提供的。

将User实体的注解注释

image.png
调用接口:http://localhost:8080/getUser image.png
如我们所愿。

也可以提供ObjectMapper

import java.text.SimpleDateFormat;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.fasterxml.jackson.databind.ObjectMapper;
@Bean
ObjectMapper objectMapper() {
    ObjectMapper om = new ObjectMapper();
    om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
    return om;
}

调用接口:http://localhost:8080/getUser

image.png
注意:这种方法是全局修改

第三种 application.yml或者properties中修改默认的配置

yml:

spring: 
  jackson: 
    date-format: yyyy-MM-dd #如果使用字符串表示,用这行设置格式
    timezone: GMT+8

properties:

spring.jackson.date-format=yyyy-MM-dd HH:mm
spring.jackson.time-zone=GMT+8

效果:


image.png

如果上面第二种和第三种方式配置同时存在,第二种会起作用。

如果三种方式都存在的时候,以实体类中注解格式为主。

使用fastjson

二、加入依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
              <!--  将springBoot默认的json处理方式排除 -->
              <exclusions>
                <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-json</artifactId>
                </exclusion>
            </exclusions>
 </dependency>
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.58</version>
 </dependency>

使用注解

private String id;

private String username;

@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
image.png

使用java配置

@Bean
    FastJsonHttpMessageConverter FastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setDateFormat("yyyy-MM-dd");
        converter.setFastJsonConfig(config);
        return converter;
    }

相关文章

网友评论

      本文标题:springBoot使用json处理Date的几种方法(环境为2

      本文链接:https://www.haomeiwen.com/subject/qcgyactx.html