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);          // 将长整型数据转换为字符串
    }
}

三、效果图

相关推荐
GreenTea31 分钟前
GrokBot 核心成员 Lauren Tan:每月交付 2000 个 PR 的人,是怎么用 AI 的
前端·后端·架构
码事漫谈32 分钟前
FDE:一个缩写,两种命运
后端
名字还没想好☜2 小时前
Go context.AfterFunc 实战(Go 1.21):context 一取消就自动跑清理,告别手写 goroutine 监听 Done
后端·golang·go
明月_清风3 小时前
为什么最近开始关注 JEV?几个实战案例告诉你答案
人工智能·后端
GetcharZp3 小时前
5 分钟拥有自己的 S3:RustFS 上手(MinIO 的 Rust 替代,Apache 2.0 可商用)
后端
行百里er4 小时前
Redis 版本演进、新特性与协议那些事儿
redis·后端
Joy T4 小时前
Spring AI 2.0 Agent 进阶:Memory、State 与 Context Engineering 常见技术全景
java·人工智能·后端·spring·agent入门·agent state
打工仔折腾 AI5 小时前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
IT_陈寒5 小时前
Java空指针这次真把我坑惨了
前端·人工智能·后端