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

三、效果图

相关推荐
tachibana216 分钟前
Go 语言的 Map 是否是并发安全的?
开发语言·数据结构·后端·golang·go·语言
dd聊技术18 分钟前
发布前的那十几秒:AI 预检为什么不用「主编 Agent」
后端
Bazingga24 分钟前
LLM记忆实战:从一次164秒慢问,到上下文治理与记忆版图
后端
看浪的路人26 分钟前
第8讲:RAG 生产化——Go 实现亿级向量召回 + 混合检索 + 重排序 Pipeline
开发语言·后端·golang
tachibana228 分钟前
Go 语言 Map 的扩容过程是怎样的?
开发语言·后端·golang·go·语言
念何架构之路30 分钟前
go-grpc服务端调用链
开发语言·后端·golang
凤山老林33 分钟前
Spring Boot 集成 Spring Modulith:搭一个能拆微服务的模块化单体
spring boot·spring·微服务
羑悻36 分钟前
atomic凭什么线程安全?无锁编程为何有时比锁还慢?C++高并发必懂的原子操作真相!
后端
SamDeepThinking38 分钟前
synchronized 和 lock 有什么区别?
java·后端·面试
m0_5873830044 分钟前
无人自助拼豆系统的全栈开发方案
java·后端·struts·小程序·架构