Spring-webflux实战

1. pom.xml引入以下内容,主要是支持在响应式的环境下对关系型数据库进行访问

        
            org.springframework.boot
            spring-boot-starter-data-r2dbc
        

        
            com.github.jasync-sql
            jasync-r2dbc-mysql
            1.1.3
        

注意: 这里对于SpringBoot的版本似乎是有一些要求的,为了避免遇到无法正常启动的情况,建议使用版本2.3.0-RELEASE版本

这里附上完整的xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.0.RELEASE
         
    
    cn.lazyfennec
    webflux-demo
    0.0.1-SNAPSHOT
    webflux-demo
    Demo project for Spring Boot
    
        1.8
        UTF-8
        UTF-8
        2.3.0.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter-webflux
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-data-r2dbc
        

        
            com.github.jasync-sql
            jasync-r2dbc-mysql
            1.1.3
        

    

    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                ${spring-boot.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


2. 修改application.yml

spring:
  r2dbc:
    url: r2dbcs:mysql://localhost:3306/test
    username: root
    password: 123456

3. 创建实体类User

package cn.lazyfennec.webfluxdemo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/25 23:15
 */
@Table("t_user")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @Id
    private Long id;

    private String name;

}

4. 修改UserController

package cn.lazyfennec.webfluxdemo.controller;

import cn.lazyfennec.webfluxdemo.dao.UserDao;
import cn.lazyfennec.webfluxdemo.model.User;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.stream.IntStream;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/25 22:13
 */
@RestController
@Slf4j
@AllArgsConstructor
public class UserController {

    private final UserDao userDao;

    @GetMapping("user")
    public Flux findAll(){
        return userDao.findAll();
    }

    @PostMapping("save")
    public Mono save(@RequestBody User user){
        return userDao.save(user);
    }

    @DeleteMapping("user/{id}")
    public Mono delete(@PathVariable Long id){
        return userDao.deleteById(id);
    }

    @GetMapping("user/{id}")
    public Mono findById(@PathVariable Long id){
        return userDao.findById(id);
    }

}

5. 启动类上方增加 @EnableR2dbcRepositories

package cn.lazyfennec.webfluxdemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;

@SpringBootApplication
@EnableR2dbcRepositories // 必须新增这个
public class WebfluxDemoApplication {

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

6. 启动然后访问相关网址测试


其实除了以上的方式之外,还有另外的方式,即handler 和 router (路由的)方式

新增以下两个类

  • UserHandler
package cn.lazyfennec.webfluxdemo.handler;

import cn.lazyfennec.webfluxdemo.dao.UserDao;
import cn.lazyfennec.webfluxdemo.model.User;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/26 0:25
 */
@Component
@AllArgsConstructor
public class UserHandler {

    private final UserDao userDao;

    //http://localhost:8080/save
    public Mono saveUser(ServerRequest request) {
        Mono mono = request.bodyToMono(User.class);
        User user = mono.block();
        return ServerResponse.ok().build(userDao.save(user).then());
    }

    public Mono deleteUser(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        return ServerResponse.ok().build(userDao.deleteById(id).then());
    }

    public Mono getUserbyId(ServerRequest request) {
        Long id = Long.valueOf(request.pathVariable("id"));
        Mono mono = userDao.findById(id);
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(mono, User.class);
    }

    public Mono listUser(ServerRequest serverRequest) {
        Flux userFlux = userDao.findAll();
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(userFlux, User.class);
    }

}
  • UserRouter
package cn.lazyfennec.webfluxdemo.config;

import cn.lazyfennec.webfluxdemo.handler.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * @Author: Neco
 * @Description:
 * @Date: create in 2022/7/26 0:27
 */
@Configuration
public class UserRouter {

    @Bean
    public RouterFunction routUser(UserHandler userHandler) {
        return RouterFunctions
                .route(RequestPredicates.GET("/user")
                        .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::listUser)
                .andRoute(RequestPredicates.GET("/user/{id}")
                        .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::getUserbyId)
                .andRoute(RequestPredicates.DELETE("/user/{id}")
                        .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::deleteUser);
    }

}
  • 注释掉UserController

访问同样的内容

正常

Tips

官方并不建议使用mysql和webflux相结合,主要是事务方面会产生一定的问题。

关于webflux,在国内其实会使用到的几率并不是很大,所以大致了解一些简单的使用基本就可以了。


如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~

你可能感兴趣的:(Spring-webflux实战)