Date 类型

前端传给后端

  1. @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
  2. private Date updateTime;

后端传给前端

  1. application.yml配置
  1. spring:
  2. ...
  3. jackson:
  4. date-format: yyyy-MM-dd HH:mm:ss
  5. time-zone: GMT+8
  6. ...
  1. 字段加上注解
  1. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
  2. private Date updateTime;

LocalDateTime类型

前端传给后端

GET请求

  1. @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
  2. private LocalDateTime updateTime;

POST请求

  1. 字段加上注解 (com.fasterxml.jackson.databind.annotation.JsonDeserialize )

    1. @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
    2. private LocalDateTime updateTime;

    或者

    1. @JsonDeserialize(using = DateTimeDeserializer.class)
    2. private LocalDateTime updateTime;
  2. 配置类

    1. /**
    2. * 参数配置
    3. * @author guojianfeng
    4. * @date 2019/10/16
    5. */
    6. public class DateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    7. @Override
    8. public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    9. if (StringUtils.isBlank(jsonParser.getText())){
    10. return null;
    11. }
    12. LocalDateTime localDateTime = LocalDateTime.parse(jsonParser.getText(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    13. return localDateTime;
    14. }
    15. }

后端传给前端

  1. 添加依赖

    1. <dependency>
    2. <groupId>com.fasterxml.jackson.datatype</groupId>
    3. <artifactId>jackson-datatype-jsr310</artifactId>
    4. <version>2.8.9</version>
    5. </dependency>
  2. 添加配置 ``` package com.clamc.valuationdisclosure.config;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;

/**

  • 描述:日期统一处理 *
  • @author guojianfeng at 2019/9/15 16:09 */ @Configuration public class WebDateConfig { @Value(“${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}”) private String pattern;

    @Bean public LocalDateTimeSerializer localDateTimeDeserializer() {

    1. return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern));

    }

    @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {

    1. return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());

    } }

  1. 2. 当然因为后端传给前端是用的RestController,也就是序列化的方式,也是可以采用前端传给后端POST的请求的方式。
  2. <a name="1PFCm"></a>
  3. ### 和数据库匹配
  4. 1. 添加依赖
  5. ```xml
  6. <dependency>
  7. <groupId>org.mybatis</groupId>
  8. <artifactId>mybatis-typehandlers-jsr310</artifactId>
  9. <version>1.0.1</version>
  10. </dependency>