简体   繁体   中英

Spring Boot R2DBC fails to store character field in MariaDB

I am buildinga REST API using Spring Reactive + MariaDB.
I am trying to store a User object. It has these properties. emailAddress (String), fullName (String), password (String) Equivalent columns in database are: 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.

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:

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.
This is the definition of my table in 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).

<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 . Did you try to change the type of your status field from char to String? Character is a specific type, there are can surprises.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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