参考: @JsonFormat 实现原理
Jackson注解@JsonFormat之Shape属性_一路有你不孤单的博客-CSDN博客
快速开始 | MyBatis-Plus
postman 发送json请求
sql:
CREATE
springboot配置:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/medb?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
maven:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mvcdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mvcdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<fastjson.version>1.2.70</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--注意和数据库版本对应-->
<version>5.1.31</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
do
@Data
mapper:
public
controller:
@Controller
main:
@SpringBootApplication
然后最后的总结:
do上的@jsonformat是在controller层返回数据到前端的时候,把"2020-11-02T16:05:30"转成"2020-11-02 16:05:30"进行接收。这个应该是springmvc返回参数时对注解进行的处理,MappingJackson2HttpMessageConverter json消息转换器处理参数。实际上就是获取注解上的pattern参数,然后使用类似DateTimeFormatter的类使用获取的pattern去解析数据库直接返回的"2020-11-02T16:05:30",然后得到"2020-11-02 16:05:30"。现在已经理解从数据库返回到前端的过程。
然后是前端到后端,前端传 "2020-11-02 16:05:30" ,然后经过controller后 (此时有jsonformat注解)自动转为"2020-11-02T16:05:30"然后拿去查数据库也是拿这个去查,数据库返回处理的也是这个。 然后当没有jsonformat注解的时候,直接传"2020-11-02 16:05:30"是没办法自动转为LocalDateTime。报 Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-11-02 16:05:30"。 然后如果不加jsonformat注解,传"2020-11-02T16:05:30"的话,也是可以正常处理的。 所以这个注解在controller层这里有个双向处理。
postman截图:
然后如果是使用"2020-11-02"和localdate的话,前端传到后端,不用jsonformat注解也可以自动转localdate。数据库出来到前端也都不需要另外的手动转。如果是其它格式的话转localdate的话,应该就需要转了。