springboot实现gpt的eventstream案例

springboot实现gpt的eventstream案例

一、maven坐标

引入webflux依赖

复制代码
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

二、democontroller

Java 复制代码
package org.example.webfluxdemo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import java.time.Duration;

@RestController
public class EventStreamController {

    @GetMapping(value = "/events", produces = "text/event-stream")
    public Flux<String> events() {
        return Flux
                .interval(Duration.ofSeconds(1))  // 每1秒产生一个数据
                .map(l -> "Data: " + l);          // 将长整型数据转换为字符串
    }
}

三、效果图

相关推荐
mldong7 小时前
从 mldong 到 jeeflow:一个工作流引擎的独立进化
后端
陈随易7 小时前
MoonBit抓包模块pcap,查看电脑的每一次联网通信
前端·后端·程序员
Aaron - Wistron8 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境8 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境8 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
IT_陈寒10 小时前
Vite热更新失效?你可能漏了这个配置
前端·人工智能·后端
SomeB1oody11 小时前
【RustyML入门】1.0. 快速上手
开发语言·后端·机器学习·rust·教程
阿文和她的Key11 小时前
GPT-5.6 降价后, API 账单的三层漏斗该怎么拆
人工智能·gpt·ai·chatgpt