spring 我如何用java编写代码来接收一个text/event-stream事件流并逐个打印它?

cvxl0en2  于 2023-03-07  发布在  Spring
关注(0)|答案(1)|浏览(1862)

我的要求是通过post发送一个请求来调用API接口,然后逐个打印服务器返回的文本/事件流事件流,不管我使用的是HttpURLConnection、WebFlux、或者RestTemplate,客户端等待服务器的完整响应,然后将数据合并成一块打印出来,无法实现一块一块返回的效果。有什么解决方案可以满足我的需求吗?这是我通过crul发送请求时得到的结果:

{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "1", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "2", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "3", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "4", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "5", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "6", "index": 0, "logprobs": null, "finish_reason": null}]}
{"id": "cmpl-6olAPoaWSKGOEj9JBlnEipVsKLblW", "object": "text_completion", "created": 1677555729, "choices": [{"text": "7", "index": 0, "logprobs": null, "finish_reason": null}]}

这是我使用HttpURLConnection编写的代码:

HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
    urlConnection.setDoOutput(true);
    urlConnection.setDoInput(true);
    urlConnection.setUseCaches(false);
    urlConnection.setRequestMethod("POST");  
    urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
    urlConnection.setRequestProperty("Connection", "Keep-Alive");
    urlConnection.setRequestProperty("Charset", "UTF-8");
    urlConnection.setRequestProperty("Authorization", "Bearer " + token);
    urlConnection.setRequestProperty("Accept", "text/event-stream");
    urlConnection.getOutputStream().write(JSON.toJSONString(completionRequest).getBytes(StandardCharsets.UTF_8));

    InputStream inputStream = urlConnection.getInputStream();
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        System.out.println("lien:"+line);
    }

这是我用webflux写的代码:

HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.setContentType(MediaType.parseMediaType("application/json; charset=UTF-8"));
  httpHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
  httpHeaders.add("Authorization", "Bearer " + token);
  HttpEntity<String> formEntity = new HttpEntity<String>(completionJSON, httpHeaders);

  Flux.create(emitter -> {
        try {
            List<String> resultList = Arrays.stream(restTemplate.postForObject(url, formEntity, String.class).split("\n")).
                    filter(line -> !line.trim().equals("")).collect(Collectors.toList());
            for (String result : resultList) {
                String message = result.replace("data: ", "");
                if (message.equals("[DONE]")) {
                    return;
                } else {
                    JSONArray choicesJSON = new JSONObject(message).getJSONArray("choices");
                    Object text = new JSONObject(choicesJSON.get(0).toString()).get("text");
                    emitter.next(text);
                }
            }
        } catch (Exception ex) {
            emitter.error(ex);
        }
    }).subscribe(System.out::println);

但在这两种情况下,输出如下所示:

{"id": "cmpl-6onbJlmNNZBShLvHeJhdOMRAhR2x6", "object": "text_completion", "created": 1677565085, "choices": [{"text":1 2 3 4 5 6 7", "index": 0, "logprobs": null, "finish_reason": null}]}

很明显,结果已经被合并,而不是被逐条返回打印,有没有办法让客户端一条条接收数据并打印出来?

siv3szwd

siv3szwd1#

假设服务器使用text/event-stream媒体类型返回数据,您可以使用WebClient使用流,然后作为Flux发出。

@GetMapping(path = "/client", produces = TEXT_EVENT_STREAM_VALUE)
Flux<String> client() {
        return webclient.post()
                .uri("/server")
                .accept(MediaType.TEXT_EVENT_STREAM)
                .header(HttpHeaders.CONNECTION, "keep-alive")
                .retrieve()
                .bodyToFlux(String.class);
}

您可以通过模拟来自同一控制器的服务器事件来测试它

@PostMapping(path = "/server", produces = TEXT_EVENT_STREAM_VALUE)
Flux<String> server() {
    return Flux.interval(Duration.ofSeconds(1))
         .map(i -> "value-" + i);
}

下面是要验证的curl命令

curl -v -H "Accept: text/event-stream" http://localhost:8080/client

*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /client HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.85.0
> Accept: text/event-stream
> 
< HTTP/1.1 200 OK
< transfer-encoding: chunked
< Content-Type: text/event-stream;charset=UTF-8
< 
data:value-0

data:value-1

data:value-2

相关问题