Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
Server-Sent Event Client example in Spring

Our team has developed Server-Sent events (SSE) for a specific task.

I am trying to build a client to listen to event streams from our server. I have sort of managed to do this using the Jersey libraries for Java. However since most of our client code uses Spring, I would like to have an example of how this could be done using Spring.

I was able to find many examples on SSE on the server side for Spring. However I am unable to find any documentation for the client side.

Does Spring support SSE on the client side? If yes, may I have an example of how the following can be achieved using Spring...

Send an HTTP GET request to our server...
GET -> http://example.com/api/events/
headers ->
Accept:text/event-stream
sessionKey:someString

The response will be a text stream, that the client will continue to receive, until the client chooses to close the connection.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Have a look at one of the execute methods of the Spring RestTemplate. They all take a ResponseExtractor as parameter. This callback interface defines one method: extractData(ClientHttpResponse response). By providing your own ResponseExtractor you can do what you want with the response, like reading from it line-by-line. Naive example:

restTemplate.execute(a_url, HttpMethod.GET, request -> {
        }, response -> {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody()));
            String line;
            try {
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println("Got some data, let's use my ObjectMapper to parse into something useful!");
                }
            } catch (IOException e) {
                //Something clever
            }
            return response;
        });
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda