spring-webflux - R2DBC - IllegalArgumentException:无法编码 java.util.Date 类型的参数
问题描述
我正在使用 R2DBC MySQL 学习 Reative jdbc。我有一个这样的存储库:
public interface VerificationTokenRepository extends ReactiveCrudRepository<VerificationToken,Long> {
VerificationToken 类如下所示:
@Table
public class VerificationToken {
@Id
private final Long id;
@Column
private final String code;
@Column
private final Date validUntil;
@Column
private boolean used;
@Column
private Long userId;
@Column
private Long verificationTokenType;
创建表的脚本是这样的:
create table verification_token (
id int unsigned not null AUTO_INCREMENT,
code varchar(36) not null,
valid_until datetime not null,
used boolean not null,
verification_token_type_id int unsigned not null,
user_id int unsigned,
PRIMARY KEY (id),
constraint verification_token_type_fk FOREIGN KEY (verification_token_type_id) REFERENCES verification_token_type(id),
CONSTRAINT user_id_fk FOREIGN KEY (user_id) REFERENCES user(id)
);
当我执行方法verifyTokenRepository.save时,控制台显示此错误:
Caused by: java.lang.IllegalArgumentException: Cannot encode parameter of type java.util.Date
at io.r2dbc.h2.codecs.DefaultCodecs.encode(DefaultCodecs.java:73)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
|_ checkpoint ⇢ SQL "INSERT INTO verification_token (code, valid_until, used, user_id, verification_token_type) VALUES ($1, $2, $3, $4, $5)" [DatabaseClient]
Stack trace:
POM 文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<!-- For testing possibility -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>0.8.2.RELEASE</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
</dependency>
如何使用 R2DBC 存储日期?或者如果 R2DBV 支持它?
解决方案
而不是 Date 使用 Instant from import java.time.Instant 包。
推荐阅读
- emeditor - 如何在 Emeditor 中进行负列搜索?
- typescript - 如何在 Typescript 中创建 UUID 模板文字类型?
- javascript - threejs 'tangent' : 未声明的标识符
- ruby-on-rails - Rails 6.1:通过 has_many 关系创建包含 .first 或 .last 的范围
- r - 在散点图上添加 r 平方和方程
- c++ - 带有特征库的法线向量的 2D 变换
- vue.js - Vue storybook globalTypes 没有重新渲染预览
- c++ - 是否可以调用具有显式特化的模板化 lambda?
- python - 如何通过 BeautifulSoup 从文本文件中解析多个 URL?
- r - 从 lmer 中找到 Y=0 的 X ± SD