Spring Boot 2.2 中的 r2dbc-postgresql 0.8.0.RC1 无法正常工作

Posted

技术标签:

【中文标题】Spring Boot 2.2 中的 r2dbc-postgresql 0.8.0.RC1 无法正常工作【英文标题】:r2dbc-postgresql 0.8.0.RC1 in Spring Boot 2.2 not working properly 【发布时间】:2020-04-12 13:53:04 【问题描述】:

在我更改了 spring webflux(spring boot 2.2.0.m3 到 2.2.2.RELEASE)和 r2dbc(spring-data-r2dbc 1.0.0.M2 到 1.0.0.RELEASE)和 r2dbc- 的版本之后postgresql (1.0.0.M7 到 0.8.0.M8) 项目我不断收到这个错误

java.lang.IllegalArgumentException: Identifier 'password' is not a valid identifier. Should be of the pattern '\$([\d]+)'.
at io.r2dbc.postgresql.ExtendedQueryPostgresqlStatement.getIndex(ExtendedQueryPostgresqlStatement.java:197) ~[r2dbc-postgresql-0.8.0.RC2.jar:0.8.0.RC2]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: Error has been observed at the following site(s):
|_ checkpoint ⇢ SQL "SELECT * FROM rc_user_account WHERE email = $1 AND  password = $2" [DatabaseClient]

我的 pom.xml 在这里

<?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.2.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.netax</groupId>
<artifactId>...</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>...</name>
<description>..</description>

<properties>
    <flyway.version>6.1.1</flyway.version>
    <java.version>1.8</java.version>
    <snippetsDirectory>$project.build.directory/generated-snippets</snippetsDirectory>
    <jwt.version>0.10.5</jwt.version>
    <spring-restdocs.version>2.0.3.RELEASE</spring-restdocs.version>
    <postgres.driver.version>9.3-1104-jdbc41</postgres.driver.version>
    <!-- test server fields -->
</properties>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Moore-SR3</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>$jwt.version</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-impl</artifactId>
        <version>$jwt.version</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-jackson</artifactId>
        <version>$jwt.version</version>
        <scope>runtime</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>

    <!-- other dependency elements omitted -->

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-r2dbc</artifactId>
        <version>1.0.0.RELEASE</version>
    </dependency>

    <!-- a R2DBC driver -->
    <!--        <dependency>-->
    <!--            <groupId>io.r2dbc</groupId>-->
    <!--            <artifactId>r2dbc-h2</artifactId>-->
    <!--            <version>1.0.0.M6</version>-->
    <!--            <exclusions>-->
    <!--                <exclusion>-->
    <!--                    <groupId>com.h2database</groupId>-->
    <!--                    <artifactId>h2</artifactId>-->
    <!--                </exclusion>-->
    <!--            </exclusions>-->
    <!--        </dependency>-->

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <!--        <dependency>-->
    <!--            <groupId>com.h2database</groupId>-->
    <!--            <artifactId>h2</artifactId>-->
    <!--            <version>1.4.197</version>-->
    <!--        </dependency>-->


    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>$flyway.version</version>
    </dependency>
    <dependency>
        <groupId>io.r2dbc</groupId>
        <artifactId>r2dbc-postgresql</artifactId>
        <version>0.8.0.RC2</version>
    </dependency>

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vision</artifactId>
        <version>1.74.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.Cloudmersive</groupId>
        <artifactId>Cloudmersive.APIClient.Java</artifactId>
        <version>v2.75</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.mvel/mvel2 -->
    <dependency>
        <groupId>org.mvel</groupId>
        <artifactId>mvel2</artifactId>
        <version>2.4.4.Final</version>
    </dependency>

    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.11</version>
    </dependency>

    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq</artifactId>
        <version>3.12.3</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-meta</artifactId>
        <version>3.12.3</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen</artifactId>
        <version>3.12.3</version>
    </dependency>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>$postgres.driver.version</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.projectreactor.kafka/reactor-kafka -->
    <dependency>
        <groupId>io.projectreactor.kafka</groupId>
        <artifactId>reactor-kafka</artifactId>
        <version>1.2.1.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework.kafka/spring-kafka -->
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>


</dependencies>

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
    <repository>
        <id>repository.spring.milestone</id>
        <name>Spring Milestone Repository</name>
        <url>http://repo.spring.io/milestone</url>
    </repository>
    <repository>
        <id>central</id>
        <name>Maven Central</name>
        <url>https://repo1.maven.org/maven2</url>
    </repository>

</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-snapshots</id>
        <url>https://repo.spring.io/snapshot</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestones</id>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

<build>
    <plugins>
        <plugin>
            <groupId>org.asciidoctor</groupId>
            <artifactId>asciidoctor-maven-plugin</artifactId>
            <version>1.5.8</version>
            <executions>
                <execution>
                    <id>generate-docs</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>process-asciidoc</goal>
                    </goals>
                    <configuration>
                        <backend>html</backend>
                        <doctype>book</doctype>
                        <attributes>
                            <snippets>$snippetsDirectory</snippets>
                        </attributes>
                        <sourceDirectory>src/docs/asciidocs</sourceDirectory>
                        <outputDirectory>target/generated-docs</outputDirectory>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.restdocs</groupId>
                    <artifactId>spring-restdocs-asciidoctor</artifactId>
                    <version>$spring-restdocs.version</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <profiles>
                    <profile>dev</profile>
                    <profile>test</profile>
                    <profile>qa</profile>
                </profiles>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <executions>
                <!-- Replacing default-compile as it is treated specially by maven -->
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <!-- Replacing default-testCompile as it is treated specially by maven -->
                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>java-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>java-test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-maven-plugin</artifactId>
            <version>$flyway.version</version>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>$postgres.driver.version</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

我失败的业务逻辑如上

public Mono<LoggedIn> login(LoginRequest request) 

return Hashing.hashPassword(request.getPassword())
    .flatMap(hashedPassword -> 
      log.info(hashedPassword);
      return userAccountRepository.findByUsernameAndPassword(request.getUsername(), hashedPassword);
    )
    .doOnError(throwable -> log.error(throwable.getMessage(), throwable))
    .doOnNext(userAccountEntity -> log.info("Login attempt : ", userAccountEntity))
    .switchIfEmpty(Mono.error(ErrorHandlerFactory.newException(ErrorCode.USER_NOT_FOUND_ERROR)))
    .map(userAccount -> 
      String accessToken = util.generateToken(userAccount.getUuid(), Role.USER);
      return LoggedIn.builder()
          .name(userAccount.getFirstName())
          .surname(userAccount.getLastName())
          .profilePictureUrl(Optional.empty())
          .company(Company.builder().name("Netax AS").description("description here").build()) // todo fix here
          .accessToken(accessToken)
          .build(); // todo omitted feature
    );

你能帮我找出为什么会出现这个错误吗?

【问题讨论】:

【参考方案1】:

我遇到了同样的错误。 我不知道您的存储库是什么样的,但在我的:

@Query("select id, title from user where title ilike $1")
Flux<User> findAllSpecial(String title); 

error "java.lang.IllegalArgumentException: Identifier 'title' 不是一个有效的标识符。应该是模式 '\$([\d]+)'。"

@Query("select id, title from user where title ilike :title")
Flux<User> findAllSpecial(String title);

有效。

【讨论】:

以上是关于Spring Boot 2.2 中的 r2dbc-postgresql 0.8.0.RC1 无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

带有 JPA 和 R2DBC 的 Spring Boot 2.4 混合项目无法启动

如何在 Spring Boot 启动时为反应式 r2dbc 驱动程序运行 flyway 迁移

带有 MySQL 的 Spring Boot R2DBC - 异常:找不到表

使用 spring r2dbc 时找不到 javax.persistence 注释

Spring R2DBC DatabaseClient.as(…)

Spring Data r2dbc - 实体继承