Webflux注解编程模型

Webflux注解编程模型

SpringWebflux实现方式有两种:注解编程模型和函数式编程模型
使用注解编程模型方式,和之前SpringMVC使用相似,只需要把相关依赖配置到项目中,SpringBoot自动配置相关运行容器,默认情况下使用Netty服务器
说明:
SpringMVC方式实现,同步阻塞的方式,基于SpringMVC+servlet+tomcat
SpringWebflux方式实现,异步非阻塞方式,基于SpringWebflux+Reactor+Netty

  1. 创建springboot工程,引入webFlux相关依赖
    在这里插入图片描述
  2. 配置启动的端口号
    在这里插入图片描述
  3. 创建包和相关类(目录结构)
    在这里插入图片描述
    实体类User.java
package com.example.webfluxdemo2.Entity;

public class User {
    
    
    private String name;
    private String gender;
    private int age;

    public User(String name, String gender, int age) {
    
    
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public void setName(String name) {
    
    
        this.name = name;
    }

    public void setGender(String gender) {
    
    
        this.gender = gender;
    }

    public void setAge(int age) {
    
    
        this.age = age;
    }

    public String getName() {
    
    
        return name;
    }

    public String getGender() {
    
    
        return gender;
    }

    public int getAge() {
    
    
        return age;
    }
}

接口UserService

package com.example.webfluxdemo2.Service;

import com.example.webfluxdemo2.Entity.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


public interface UserService {
    
    
//    通过id 返回 0个或一个对象
    Mono<User> getUserById(int id);
//    返回多个
    Flux<User> getAllUser();
//    添加对象,没有返回值类型
    Mono<Void> addUserInfo(Mono<User> userMono);
}

接口实现类UserServiceimpl

package com.example.webfluxdemo2.Service;

import com.example.webfluxdemo2.Entity.User;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Repository
public class UserServiceimpl implements UserService{
    
    
//    创建Map集合存储数据
    private final Map<Integer,User> user=new HashMap<>();
//    实例化时调用构造方法,给map添加值
    public UserServiceimpl(){
    
    
        this.user.put(1,new User("sun","nan",18));
        this.user.put(2,new User("tom","nv",23));
        this.user.put(3,new User("jack","nan",25));

    }
//    根据id查询
    @Override
    public Mono<User> getUserById(int id) {
    
    
        return Mono.justOrEmpty(this.user.get(id));
    }
//查询多个用户
    @Override
    public Flux<User> getAllUser() {
    
    
        return Flux.fromIterable(this.user.values());
    }
//添加用户
    @Override
    public Mono<Void> addUserInfo(Mono<User> userMono) {
    
    
//        传入的对象调用doOnNext的方法,利用拉姆达表达式,将其添加到map里面。这里的person就是传入的对象。
        return userMono.doOnNext(person->{
    
    
//            向map里面添加值
            int id=user.size()+1;
            this.user.put(id,person);

        }).thenEmpty(Mono.empty());
//        空值处理,将里面内容清空,Mono和flux可以发送多个信号,信号要有终止信号,如果没有终止信号,那就是无限的数据流,把里面内容清空,就代表终止了。
    }
}

UserController.java

package com.example.webfluxdemo2.Controller;

import com.example.webfluxdemo2.Entity.User;
import com.example.webfluxdemo2.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
@RestController
public class UserController {
    
    
//    注入UserService
    @Autowired
    private UserService userService;
//    id查询
    @GetMapping("/user/{id}")
    public Mono<User> getUserId(@PathVariable int id){
    
    
        return userService.getUserById(id);
    }
    @GetMapping("/user")
    public Flux<User> GetAllUser(){
    
    
        return userService.getAllUser();
    }

    @GetMapping("/adduser")
    public Mono<Void> addUserInfo(@RequestBody User user){
    
    
        Mono<User> userMono=Mono.just(user);
        return userService.addUserInfo(userMono);

    }

}

运行效果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/sx17860543449/article/details/123639088