【问题标题】:Unable get response with R2DBC Postgresql WebFlux无法使用 R2DBC Postgresql WebFlux 获得响应
【发布时间】:2020-08-24 23:43:05
【问题描述】:

我正在学习 R2DBC 和弹簧靴 WebFluxPostgresql。我已经成功配置了在我的本地机器上运行的 PostgreSQL 数据库。我使用了以下依赖项和插件。

plugins {
    id 'org.springframework.boot' version '2.3.0.BUILD-SNAPSHOT'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id 'eclipse'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    compile group: 'io.r2dbc', name: 'r2dbc-postgresql', version: '1.0.0.M7'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    testImplementation 'io.projectreactor:reactor-test'
}

请找到我的代码

@SpringBootApplication
@EnableR2dbcRepositories
public class DemoServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoServiceApplication.class, args);
    }
}

@RestController
@RequiredArgsConstructor
class ResourceController {
    final ResourceRepository resourceRepository;

    @GetMapping("/method1") 
    public Flux<Resource> getResourcesMethod1(){
         return resourceRepository.deleteAll().thenMany(
                 Flux.just(new Resource("name1", "description1", new Date()), new Resource("name2", "description2", new Date()))
                 .flatMap(resourceRepository::save))
         .thenMany(
                 resourceRepository.findAll()
                 .flatMap(data -> {
                     return Flux.just(data);
                 }));
    }

    @GetMapping("/method2") 
    public Flux<Resource> getResourcesMethod2(){
        return resourceRepository.findAll();     
    }
}

@Configuration
class DatabaseConfig extends AbstractR2dbcConfiguration {

    @Bean
    public ConnectionFactory connectionFactory() {
        return new PostgresqlConnectionFactory(
                PostgresqlConnectionConfiguration.builder()
                        .host("localhost")
                        .port(5432)
                        .username("postgres")
                        .password("password")
                        .database("mydatabase")
                        .build());
    }

}


interface ResourceRepository extends ReactiveCrudRepository<Resource, Integer> {

}

@Data
class Resource {
    @Id
    Integer id;

    final String name;
    final String description;
    final Date createdDate;

    public Resource(String name, String description, Date createdDate) {
        this.name = name;
        this.description = description;
        this.createdDate = createdDate;
    }
}

从上面的代码(使用getResourcesMethod1)我能够成功删除数据并将数据插入到数据库表中。

但是,我无法从这两个其余端点检索到任何响应。我使用 Postman 进行测试,它无法检索任何东西...只是缓冲...

我在这里做错了什么?我可能错过了一个非常基本的东西。任何帮助将不胜感激。

编辑

如果我在getResourcesMethod2 中打印出通量,它将打印为FluxOnErrorResume 而不是FluxArray,尽管这是预期的。我认为这可能与数据库配置有关。但是我找不到这个的根课程..

【问题讨论】:

    标签: postgresql spring-boot spring-webflux project-reactor spring-data-r2dbc


    【解决方案1】:

    Postman 不支持返回流。

    有一个开放的功能请求,但自 2018 年以来一直存在。

    https://github.com/postmanlabs/postman-app-support/issues/5040

    您可以使用 curl 并通过 -N 标志禁用缓冲来流式传输响应。

    curl -N <your_url>
    

    如果你不想使用curl,只是为了测试,那么你可以collectList并返回一个Mono&lt;List&lt;T&gt;&gt;给客户端,然后使用postman。

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 2021-08-19
      • 2016-11-05
      • 2014-06-06
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多