SpringBoot2.x webflux响应式编程实战

本文介绍了SpringBoot2.x中使用WebFlux进行响应式编程的实战内容,包括添加依赖、Service业务接口及Controller类的实现。在Controller中展示了如何通过Mono和Flux处理单个和多个数据的异步操作,如延迟返回、按需获取数据等,以提高效率。

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

1、WebFlux中,请求和响应不再是WebMVC中的ServletRequest和ServletResponse,而是ServerRequest和ServerResponse

2、加入依赖,如果同时存在spring-boot-starter-web,则会优先用spring-boot-starter-web
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

 

3.Service业务接口

  

package net.xdclass.webflux_project.service;

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

import org.springframework.stereotype.Service;

import net.xdclass.webflux_project.domain.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class UserService {
   
    private static final Map<String,User> dataMap = new HashMap<>();
    
    static {
        dataMap.put("1",new User("1", "冰"));
        dataMap.put("2",new User("2", "雪"));
        dataMap.put("3",new User("3", "之"));
        dataMap.put("4",new User("4", "华"));
    }
    
    /**
     * 返回列表
     */
    public Flux<User> list(){
        Collection<User> list = this.dataMap.values();
        return Flux.fromIterable(list);
    }
    
    /**
     * 根据id查找用户
     */
    public Mono<User> getById(final String id){
        return Mono.justOrEmpty(this.dataMap.get(id));
    }
    
    /**
     * 删除用户
     */
    public Mono<User> del(final String id){
        return Mono.justOrEmpty(this.dataMap.remove(id));
    }
}
 

Controller类

package net.xdclass.webflux_project.controller;

import java.time.Duration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import net.xdclass.webflux_project.domain.User;
import net.xdclass.webflux_project.service.UserService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/v1/user")
public class UserController {
    
    @Autowired
    private UserService UserService;
    
    @GetMapping("/test")
    public Mono<String> test(){
        return Mono.just("冰雪之华");
    }
    

   //下面的注解说明是响应式编程,在界面返回的是每2秒返回一个,非阻塞的

  //相对于非响应式编程,全部查出再返回效率更高。

  //需要注意的是mysql不支持,redis等支持
    @GetMapping(value="list",produces=MediaType.APPLICATION_STREAM_JSON_VALUE)
    public Flux<User> list(){
        return UserService.list().delayElements(Duration.ofSeconds(2));
    }
    
    @GetMapping("getById")
    public Mono<User> getById(String id){
        return UserService.getById(id);
    }
    
    @GetMapping("del")
    public Mono<User> del(String id){
        return UserService.del(id);
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值