简体   繁体   English

Spring 引导 R2DBC 无法在 MariaDB 中存储字符字段

[英]Spring Boot R2DBC fails to store character field in MariaDB

I am buildinga REST API using Spring Reactive + MariaDB.我正在构建 REST API 使用 Spring 反应 + Z7F9733E208088B1396DF3D4BE176。
I am trying to store a User object.我正在尝试存储用户 object。 It has these properties.它具有这些属性。 emailAddress (String), fullName (String), password (String) Equivalent columns in database are: emailAddress (varchar), fullName (varchar), password (varchar) emailAddress (String)、fullName (String)、password (String) 数据库中的等效列是:emailAddress (varchar)、fullName (varchar)、password (varchar)

It works fine with the above fields, but when I add a new field of type ( private char status ) in my User object, and an equivalent column in my table in database ( status char(1) ), it fails with the following error.它适用于上述字段,但是当我在我的用户 object 中添加一个类型为( private char status )的新字段,并在我的数据库表中添加一个等效列( status char(1) )时,它失败并出现以下错误.

2021-02-14 | 18:37:51.904 | reactor-tcp-nio-2    | ERROR | o.s.b.a.w.r.e.AbstractErrorWebExceptionHandler | [0aef325c-1]  500 Server Error for HTTP POST "/users"
java.lang.IllegalArgumentException: No encoder for class java.lang.Character (parameter at index 3) 
    at org.mariadb.r2dbc.MariadbClientParameterizedQueryStatement.bind(MariadbClientParameterizedQueryStatement.java:93)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    |_ checkpoint ⇢ Handler com.timesheet.timesheetapi.controller.UserController#post(User) [DispatcherHandler]
    |_ checkpoint ⇢ HTTP POST "/users" [ExceptionHandlingWebHandler]
Stack trace:
        at org.mariadb.r2dbc.MariadbClientParameterizedQueryStatement.bind(MariadbClientParameterizedQueryStatement.java:93)
        at org.mariadb.r2dbc.MariadbClientParameterizedQueryStatement.bind(MariadbClientParameterizedQueryStatement.java:34)
...

This is my class:这是我的 class:

public class User {

    @Id
    private long id;

    private String email;
    @Column("fullName")
    private String fullName;
    private String password;
    private char status;
    @Column("crDate")
    private LocalDateTime crDate;
    @Column("updDate")
    private LocalDateTime updDate;
}

You can ifnore the crDate/updDate fields.您可以使用 crDate/updDate 字段。
This is the definition of my table in MariaDB.这是我的表在 MariaDB 中的定义。

CREATE TABLE `tuser` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `email` VARCHAR(100) NULL DEFAULT NULL,
    `fullName` VARCHAR(100) NULL DEFAULT NULL,
    `status` CHAR(1) NOT NULL DEFAULT 'D' COMMENT 'A=Active,D=Disabled',
    `password` VARCHAR(100) NULL DEFAULT NULL,
    `crDate` DATETIME NULL DEFAULT current_timestamp(),
    `updDate` DATETIME NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=16;

This is the relevant part of my POM (in case it helps).这是我的 POM 的相关部分(以防万一)。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mariadb</groupId>
            <artifactId>r2dbc-mariadb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
        </dependency>

I am sure I must be missing something very basic but don't know what.我确信我一定错过了一些非常基本的东西,但不知道是什么。 Any ideas?有任何想法吗?

I think your DB API can't work with type Character, about it tell message No encoder for class java.lang.Character .我认为您的 DB API 不能与字符类型一起使用,关于它告诉消息No encoder for class java.lang.Character Did you try to change the type of your status field from char to String?您是否尝试将状态字段的类型从 char 更改为 String? Character is a specific type, there are can surprises.性格是特定的类型,有惊喜。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM