Java 调试服务器在Chrome和Postman中从Spring发送事件流

Java 调试服务器在Chrome和Postman中从Spring发送事件流,java,spring,spring-webflux,project-reactor,Java,Spring,Spring Webflux,Project Reactor,根据,当返回流量时,Spring应该为订阅返回的每个元素发出一个server-sent事件 下面是一个示例REST控制器: 包myapp.controller; 导入myapp.MyOutput; 导入io.swagger.annotations.Api; 导入io.swagger.annotations.api操作; 导入io.swagger.annotations.ApiResponse; 导入io.swagger.annotations.ApiResponses; 导入org.spring

根据,当返回
流量时,Spring应该为订阅返回的每个元素发出一个server-sent事件

下面是一个示例REST控制器:

包myapp.controller;
导入myapp.MyOutput;
导入io.swagger.annotations.Api;
导入io.swagger.annotations.api操作;
导入io.swagger.annotations.ApiResponse;
导入io.swagger.annotations.ApiResponses;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.MediaType;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RestController;
导入reactor.core.publisher.Flux;
@RestController
@请求映射(“/api/v1/test”)
@Api(标签=“测试Api”)
公共类测试控制器{
@蜂房手术(
value=“测试”,
响应=MyOutput.class,
产生=MediaType.TEXT\u事件\u流\u值
)
@请求映射(
value=“”,
方法=RequestMethod.PATCH,
产生=MediaType.TEXT\u事件\u流\u值
)
@蜂鸟反应(
值={
@ApiResponse(代码=200,message=“服务执行成功”),
@ApiResponse(代码=400,消息=“输入数据错误”),
@ApiResponse(code=500,message=“发生内部服务器错误”),
@ApiResponse(code=503,message=“服务不可用”)
}
)
公众反应测试(){
返回ResponseEntity.ok().header(“连接”,“保持活动”)
.阀体(流量范围(0,1000)
.delayElements(持续时间秒(1))
.map(MyOutput::新建)
);
}
}
使用wget的响应示例:

data:{"recordCount":0}

data:{"recordCount":148}

data:{"recordCount":226}

data:{"recordCount":266}

data:{"recordCount":272}

data:{"recordCount":286}

data:{"recordCount":287}

data:{"recordCount":293}

data:{"recordCount":294}
当使用Chrome或Postman调试端点时,客户端似乎将事件解释为分块响应的一部分,而不是事件流。我已经确认响应数据是相同的,并且需要预期的时间。请参见下面Chrome网络选项卡的屏幕截图:

EventStream-选项卡为空:


将其与网站的标题进行比较,如:

其中EventStream选项卡正确显示事件:



重要的事实是,当使用
WebClient
使用Spring端点时,我可以使用
.bodyToFlux
在预期时间成功接收每个事件。因此,这些事件似乎缺少了Chrome希望从服务器发送的事件流中获得的某种形式的配置,但使用时,浏览器中的哪个?

首先将执行GET请求,因此使用修补程序在浏览器中并不真正兼容

如果我们获取您的代码并在GET中更改修补程序,我们将创建一个简单的页面:


测试SSE
const evtSource=new EventSource(“/api/v1/test”);
evtSource.onmessage=函数(事件){
const newElement=document.createElement(“li”);
const eventList=document.getElementById(“结果”);
newElement.textContent=“消息:”+event.data;
appendChild(新元素);
}

让你的应用程序为这个静态文件服务,并在chrome中打开它。您将在“事件”选项卡中正确地看到事件。但是,如果您直接请求/api/v1/test,则将在页面中而不是在事件选项卡中显示该事件。我假设event选项卡intercept EventSource对象,如果没有创建EventSource,就不会使用它。

我想我实际上也使用GET进行了测试,但是关于拦截
EventSource
对象的部分是我一直在寻找的信息。谢谢