springboot实现ChatGPT式调用(一次调用,持续返回)

下边实现了一个持续返回100以内随机数的接口,在接口超时之前会每隔1秒返回一个随机数

java 复制代码
@GetMapping(value = "/getRandomNum", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public SseEmitter getRandomNum() {
    SseEmitter emitter = new SseEmitter();
    ThreadLocalRandom random = ThreadLocalRandom.current();
    new Thread(() -> {
            try {
                while (true) {
                    int price = random.nextInt(100);
                    emitter.send(SseEmitter.event().data(price));
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        }).start();
    return emitter;
}

下边是postman调用效果图

相关推荐
马卡巴卡18 小时前
Java线程协作工具:CountDownLatch 、CyclicBarrier、Phaser、Semaphore 、Exchanger
后端
Java水解18 小时前
Spring Bean生命周期深度剖析:从创建到销毁的完整旅程
spring boot·后端
Json_18 小时前
springboot框架对接物联网,配置TCP协议依赖,与设备通信,让TCP变的如此简单
java·后端·tcp/ip
+VX:Fegn089518 小时前
计算机毕业设计|基于springboot + vue职位管理推荐系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
王中阳Go18 小时前
06 Go Eino AI应用开发实战 | Eino 框架核心架构
人工智能·后端·go
C+++Python18 小时前
Java 锁机制
java·开发语言
czlczl2002092518 小时前
Spring Security 6 :配置生产级 SecurityFilterChain
java·spring
Angletank18 小时前
SpringBoot用JPA接口实现分页和排序
windows·spring boot·后端
疯狂的程序猴18 小时前
生成加密 IPA 的工具在项目中的使用方式
后端
Java小白,一起学习18 小时前
AndroidStudio安装教程
java·android-studio