【引言】
記錄兩個最近遇到的問題:
1. SpringBoot集成MybatisPlus連接Mysql問題
(1) 問題描述
之前整合了SpringBoot+Mybatis的項目,最近想在項目中實踐一些東西,啓動項目,報錯如下:
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.baomidou.mybatisplus.generator.config.DataSourceConfig.getConn(DataSourceConfig.java:183)
從第一句中,可以定位問題,連接數據庫需要指定一個時區。
(2) 問題復現
項目中,連接數據庫的配置如下:
# DataSource Config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username=root
spring.datasource.password=123456
(3) 解決方案
修改數據庫連接配置,指定時區,如下:
# DataSource Config
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true**&serverTimezone=UTC**
spring.datasource.username=root
spring.datasource.password=123456
2. LocalDate反序列化問題
(1) 問題描述
一個簡單的修改某字段日期的接口,前端傳值爲日期格式的字符串’2020-03-25’,後端使用對象接收,報錯如下:
org.springframework.http.converter.HttpMessageConversionException:
Type definition error: [simple type, class java.time.LocalDate];
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `java.time.LocalDate`
(no Creators, like default construct, exist):
no String-argument constructor/factory method to deserialize from String value ('2020-03-25')
從最後一句中,我們可以定位問題,日期字段反序列失敗。
(2) 問題復現
定義實體類僞代碼如下:
@Data
public class infoReq{
/**
* 編號
*/
private Long id;
/**
* 日期
*/
private LocalDate payDate;
}
前端傳值如下:
{
"payDate": "2020-03-25",
"id": "22"
}
(3) 解決方案
解決問題的辦法很簡單,在實體對象字段上增加以下註解即可:
@Data
public class infoReq{
/**
* 編號
*/
private Long id;
/**
* 日期
*/
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
@JsonSerialize(using = LocalDateSerializer.class)
@JsonDeserialize(using = LocalDateDeserializer.class)
private LocalDate payDate;
}