I am learning R2DBC
with spring boot WebFlux
and Postgresql
. I have successfully configured with PostgreSQL database which is running on my local machine. I used the following dependencies and plugins.
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'
}
Please find my code
@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;
}
}
From the above code (using getResourcesMethod1
) I was able to delete the data and insert data to the database table successfully.
However I could not retrieve any response from both those rest endpoints. I have used Postman to test and it could not retrieve any thing... Just buffering...
What am I doing wrong here? I might missing a very basic thing. Any help would be grateful.
EDIT
If i print out flux in getResourcesMethod2
it will print as FluxOnErrorResume
instead of FluxArray
which is expected though. I think this might with the database configuration. But i can not find the root course for this..