代码提交

This commit is contained in:
liujing33
2025-05-08 18:02:47 +08:00
parent 55fcc338d7
commit ba04a1047b
60 changed files with 1961 additions and 201 deletions

View File

@@ -0,0 +1,50 @@
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mangmang</groupId>
<artifactId>reactive-programming</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.mangmang</groupId>
<artifactId>learning-nexus</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<!-- 确保添加 spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-data-mongodb-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,15 @@
package org.mangmang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Hello world!
*
*/
@SpringBootApplication
public class ReactiveProgrammingApplication {
public static void main(String[] args) {
SpringApplication.run(ReactiveProgrammingApplication.class, args);
}
}

View File

@@ -0,0 +1,12 @@
package org.mangmang.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}

View File

@@ -0,0 +1,74 @@
package org.mangmang.controller;
import org.mangmang.entity.Product;
import org.mangmang.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductService productService;
@Autowired
public ProductController(ProductService productService) {
this.productService = productService;
}
@GetMapping
public Flux<Product> getAllProducts() {
return productService.getAllProducts();
}
@GetMapping("/{id}")
public Mono<ResponseEntity<Product>> getProductById(@PathVariable String id) {
return productService.getProductById(id)
.map(ResponseEntity::ok)
.defaultIfEmpty(ResponseEntity.notFound().build());
}
@GetMapping("/category/{category}")
public Flux<Product> getProductsByCategory(@PathVariable String category) {
return productService.getProductsByCategory(category);
}
@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<Product> streamAllProducts() {
return productService.getAllProducts();
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Mono<Product> createProduct(@RequestBody Product product) {
return productService.saveProduct(product);
}
@PutMapping("/{id}")
public Mono<ResponseEntity<Product>> updateProduct(@PathVariable String id, @RequestBody Product product) {
return productService.getProductById(id)
.flatMap(existingProduct -> {
existingProduct.setName(product.getName());
existingProduct.setPrice(product.getPrice());
existingProduct.setCategory(product.getCategory());
return productService.saveProduct(existingProduct);
})
.map(updatedProduct -> ResponseEntity.ok(updatedProduct))
.defaultIfEmpty(ResponseEntity.notFound().build());
}
@DeleteMapping("/{id}")
public Mono<ResponseEntity<Void>> deleteProduct(@PathVariable String id) {
return productService.getProductById(id)
.flatMap(existingProduct ->
productService.deleteProduct(id)
.then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK)))
)
.defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}
}

View File

@@ -0,0 +1,15 @@
package org.mangmang.entity;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Data
@Document
public class Product {
@Id
private String id;
private String name;
private double price;
private String category;
}

View File

@@ -0,0 +1,9 @@
package org.mangmang.repository;
import org.mangmang.entity.Product;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import reactor.core.publisher.Flux;
public interface ProductRepository extends ReactiveMongoRepository<Product, String> {
Flux<Product> findByCategory(String category);
}

View File

@@ -0,0 +1,39 @@
package org.mangmang.service;
import org.mangmang.entity.Product;
import org.mangmang.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@Service
public class ProductService {
private final ProductRepository productRepository;
@Autowired
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public Flux<Product> getAllProducts() {
return productRepository.findAll();
}
public Mono<Product> getProductById(String id) {
return productRepository.findById(id);
}
public Flux<Product> getProductsByCategory(String category) {
return productRepository.findByCategory(category);
}
public Mono<Product> saveProduct(Product product) {
return productRepository.save(product);
}
public Mono<Void> deleteProduct(String id) {
return productRepository.deleteById(id);
}
}