Description:
I am developing a Spring WebFlux application that integrates with a third-party API. This API returns data in the text/event-stream
format, specifically for streaming chat messages. I have set up an endpoint in my application to forward requests to this API using WebClient and handle the SSE responses.
Problem:
Despite receiving data from the third-party API when testing with tools like Postman and cURL, my Spring WebFlux endpoint (/v1/api/bot-manager/chat-messages
) does not return any data to the client. I have verified that the third-party API responds correctly with the expected SSE data format when called directly.
Code Snippets:
Below are relevant parts of my Spring WebFlux application:
-
Service Layer (
BotManagerService
):@Override public Flux<String> chatMessages(ChatMessageRequest model) { BotInfo botInfo = botInfoRepository.findById(model.getBotId()).orElse(null); if (botInfo != null) { HttpHeaders headers = new HttpHeaders(); headers.setBearerAuth(botInfo.getBotKey()); headers.setContentType(MediaType.APPLICATION_JSON); return webClient.post() .uri("/chat-messages") .headers(httpHeaders -> httpHeaders.addAll(headers)) .body(BodyInserters.fromValue(model)) .accept(MediaType.TEXT_EVENT_STREAM, MediaType.APPLICATION_OCTET_STREAM, MediaType.APPLICATION_JSON) .acceptCharset(StandardCharsets.UTF_8) .retrieve() .bodyToFlux(String.class) .doOnNext(chatResponse -> { System.out.println(gson.toJson(chatResponse)); }) .log(); } throw new RuntimeException("Bot info not found"); }
-
Controller Layer (
BotManagerController
):@PostMapping(value = "/chat-messages", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public Flux<String> chatMessages(@RequestBody ChatMessageRequest model) { return botManagerService.chatMessages(model); }
Additional Information:
- The third-party API returns data continuously in the
text/event-stream
format. - I have confirmed that the
webClient
setup and headers are correct since I can log the responses in my service layer. - Despite successful logs in the service layer, the controller endpoint does not transmit any data back to the client.
Question:
What could be causing my Spring WebFlux endpoint /v1/api/bot-manager/chat-messages
to not transmit the received Server-Sent Events (SSE) data to the client? Is there a configuration issue or an error in my approach?