响应式WebFlux的CURD

本文介绍了如何在Spring WebFlux框架下进行响应式MongoDB数据库操作,包括依赖配置、@EnableReactiveMongoRepositories注解的使用,以及@Document指定MongoDB的集合。同时,展示了如何通过save方法实现增删改查,并利用Hibernate的参数校验增强数据安全性。此外,还提到了使用切面添加校验注解的细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

响应式数据库无法用传统的数据库,这里用MongoDB:

1.加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>

2.加注解

@EnableReactiveMongoRepositories

3.加配置

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/webflux

实体类:

@Document(collection = "user")
@Data
public class User {
    @Id
    private String id;
    private String name;
    private int age;
}

@Document(collection = "user")表示是mongodb的表,表名是user

对应mongo接口:

@Repository
public interface UserRepository extends ReactiveMongoRepository<User, String> {
}

实现:

@RestController
@RequestMapping("/user")
public class UserController {
    private final UserRepository userRepository;

    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @GetMapping("/")
    public Flux<User> getAll(){
        return userRepository.findAll();
    }
    @GetMapping(value = "/stream" , produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<User> getStreamAll(){
        return userRepository.findAll();
    }

    @PostMapping("/")
    public Mono<User> createUser(@RequestBody User user){
        return this.userRepository.save(user);
    }

    /**
     * 根据id删除用户
     * 存在返回200,不存在返回404
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Mono<ResponseEntity<Void>> deleteUser(@PathVariable("id") String id){
        //deleteById是没有返回值的,无法知道是否删除成功,所以不能用这个
//        this.userRepository.deleteById(id)
        return this.userRepository.findById(id)
                //当要操作数据,并返回一个Mono,用flatMap
                //如果不操作数据,只是转换数据,用map
                .flatMap(user -> this.userRepository.delete(user)
                    .then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))))
                .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @PutMapping("/{id}")
    public Mono<ResponseEntity<User>> updateUser(
            @PathVariable("id") String id,
            @RequestBody User user
    ){
        return this.userRepository.findById(id)
                //flat 操作数据
                .flatMap(u -> {
                    u.setAge(user.getAge());
                    u.setName(user.getName());
                    return this.userRepository.save(u);
                })
                //map 转换数据
                .map(u -> new ResponseEntity<User>(u , HttpStatus.OK))
                .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    @GetMapping("/{id}")
    public Mono<ResponseEntity<User>> findUserById(@PathVariable("id") String id){
        return this.userRepository.findById(id)
                .map(u -> new ResponseEntity<User>(u , HttpStatus.OK))
                .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
}

需要注意的是在spring data jpa中,新增和修改都是save,有id是修改,没有id是新增。


还可以对参数进行校验(用hibernate),依赖:

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.6.Final</version>
        </dependency>

代码:

@Document(collection = "user")
@Data
public class User {
    @Id
    private String id;
    @NotBlank
    private String name;
    @Range(min = 10 , max = 100)
    private int age;
}

新增切面:

/**
 * 异常处理切面
 */
@ControllerAdvice
public class CheckAdvice {
    @ExceptionHandler(WebExchangeBindException.class)
    public ResponseEntity handleBindException(WebExchangeBindException exception){
        return new ResponseEntity<String>(toStr(exception) , HttpStatus.BAD_REQUEST);
    }

    private String toStr(WebExchangeBindException exception) {
        return exception.getFieldErrors().stream()
                .map(e -> e.getField() + ":" + e.getDefaultMessage())
                .reduce("" , (s1 , s2) -> s1 + "\n" + s2);
    }
}

在需要校验单的接口加注解:

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值