In this tutorial, I want to show you how to perform various Postgres CRUD operations through Spring Data R2DBC with Spring WebFlux.
R2DBC stands for reactive relational database connections.
Like JPA (Java persistence API), R2DBC is the specification of the reactive drivers of relational databases. Since it is a separate specification, don't compare it with JPA/Hibernate features (like @OneToMany, @ManyToMany, etc.).
We will develop a Spring Boot application called product-service, which is responsible for creating new products/retrieving all products/deleting or updating existing products to perform various Postgres CRUD operations for R2DBC.
Entity Class
@Data @ToString <b>public</b> <b>class</b> Product { @Id <b>private</b> Integer id; <b>private</b> String description; <b>private</b> Double price; }
We can't add @Entity here because this is not JPA.
Spring Data Reactive Repository
Spring Data does all the heavy lifting as usual. We need to create a repository for our entity class by extending ReactiveCrudRepository.
<b>import</b> ; <b>import</b> ; @Repository <b>public</b> <b>interface</b> ProductRepository <b>extends</b> ReactiveCrudRepository<Product, Integer> { }
CRUD operation
Let's create a service class to perform Postgres CRUD operations through Spring Data Reactive Repository.
@Service <b>public</b> <b>class</b> ProductService { @Autowired <b>private</b> ProductRepository repository; <b>public</b> Flux<Product> getAllProducts(){ <b>return</b> <b>this</b>.(); } <b>public</b> Mono<Product> getProductById(<b>int</b> productId){ <b>return</b> <b>this</b>.(productId); } <b>public</b> Mono<Product> createProduct(<b>final</b> Product product){ <b>return</b> <b>this</b>.(product); } <b>public</b> Mono<Product> updateProduct(<b>int</b> productId, <b>final</b> Mono<Product> productMono){ <b>return</b> <b>this</b>.(productId) .flatMap(p -> (u -> { (()); (()); <b>return</b> p; })) .flatMap(p -> <b>this</b>.(p)); } <b>public</b> Mono<Void> deleteProduct(<b>final</b> <b>int</b> id){ <b>return</b> <b>this</b>.(id); } }
REST API
Now it's time to publicly serve through the REST API:
@RestController @RequestMapping(<font>"product"</font><font>) <b>public</b> <b>class</b> ProductController { @Autowired <b>private</b> ProductService productService; @GetMapping(</font><font>"all"</font><font>) <b>public</b> Flux<Product> getAll(){ <b>return</b> <b>this</b>.(); } @GetMapping(</font><font>"{productId}"</font><font>) <b>public</b> Mono<ResponseEntity<Product>> getProductById(@PathVariable <b>int</b> productId){ <b>return</b> <b>this</b>.(productId) .map(ResponseEntity::ok) .defaultIfEmpty(().build()); } @PostMapping <b>public</b> Mono<Product> createProduct(@RequestBody Mono<Product> productMono){ <b>return</b> (<b>this</b>.productService::createProduct); } @PutMapping(</font><font>"{productId}"</font><font>) <b>public</b> Mono<Product> updateProduct(@PathVariable <b>int</b> productId, @RequestBody Mono<Product> productMono){ <b>return</b> <b>this</b>.(productId, productMono); } @DeleteMapping(</font><font>"/{id}"</font><font>) <b>public</b> Mono<Void> deleteProduct(@PathVariable <b>int</b> id){ <b>return</b> <b>this</b>.(id); } } </font>
Configuration
Spring Data Reaction Driver requires such configuration to connect to Postgres DB.
Method 1: Use
spring.=r2dbc:postgresql:<font><i>//localhost:5432/productdb</i></font><font> spring.=vinsguru spring.=admin </font>
Method 2: Open connection factory bean
@Configuration <b>public</b> <b>class</b> R2DBCConfig { @Bean <b>public</b> ConnectionFactory connectionFactory() { <b>return</b> ( () .option(DRIVER, <font>"postgresql"</font><font>) .option(HOST, </font><font>"localhost"</font><font>) .option(PORT, 5432) .option(USER, </font><font>"vinsguru"</font><font>) .option(PASSWORD, </font><font>"admin"</font><font>) .option(DATABASE, </font><font>"productdb"</font><font>) .option(MAX_SIZE, 40) .build()); } } </font>
The complete source code ishere。
This is the article about using Spring Data R2DBC + Postgres to implement the addition, deletion, modification and search function. For more related Spring Data R2DBC + Postgres to implement the addition, deletion, modification and search content, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!