2

i want to use the java websocket API in a Spring Boot application. I created the following class as described here: https://www.baeldung.com/java-websockets

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint(value = "/test")
public class FrontendEndpoint {

        @OnOpen
        public void onOpen(Session session) throws IOException {
                session.getBasicRemote().sendText("Test");
        }

        @OnMessage
        public void onMessage(Session session, String message) throws IOException {
        }

        @OnClose
        public void onClose(Session session) throws IOException {
        }

        @OnError
        public void onError(Session session, Throwable throwable) {
        }
}

I try to connect to that websocket but nothing happens. I saw a lot of articles in the internet but nothing helped me. I dont know how to get it to work.

When i try to connect to the websocket nothing happend.

Spring Boot version: 2.0.3

Websocket-API version: 1.1

I also don't see an open port for the websocket.

Thanks

BR

1
  • could you tell me how did you configure javax websocket endpoints. Any example projects fo r that. Commented May 23, 2020 at 12:37

3 Answers 3

5

It's a bit late, but in case someone will come here for a solution.

It is not covered in Spring documentation how to use Java WebSocket API (JSR-356) in Spring and I didn't manage to find any tutorial describing how to achieve it despite I've spent several hours googling it.

In order to use Java WebSocket API with Spring you should use class org.springframework.web.socket.server.standard.SpringConfigurator: https://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/web/socket/server/standard/SpringConfigurator.html from

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-websocket</artifactId>
   <version>...</version>
</dependency>

Just add it to your

@ServerEndpoint(... configurator = SpringConfigurator.class) 

and it is done. Your annotated class becomes a usual Spring-managed component legal for @Autowired injection and so on.

Hope it helps someone :)

UPD.

It should be mentioned, that JSR 356 WebSocket initialization through the SpringConfigurator is only supported in .war projects, that are to be deployed as usual artifacts in servlet container.

It will not work as expected when project is packaged in executable Spring Boot .jar or Spring Boot executable .war and when it is deployed it standalone mode via java -jar {app}

I didn't manage to find a way to initialize JSR 356 WebSocket with @Autowired Spring components in it in Spring Boot environment in executable archive.

It is possible to deploy JSR 356 WebSocket via ServerEndpointExporter.setAnnotatedEndpointClasses(...), but every class provided must have a no-arg constructor otherwise exception will be thrown. @Autowired field will not be processed by Spring in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

would you please give an example. a github repo maybe? thanks in advance
4

I once created a sample application with Spring Boot and Websocket API

https://github.com/simasch/spring-boot-websocket

but the problem is that the ServerEndpoint is not managed by Spring but by the Websocket implementation. So this is maybe not the way you should use websockets with Spring Boot.

I would recommend to have a look how Spring think that Websockets should be used: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket

3 Comments

It is working! Thanks. I had to add the configuration class as you do in your example. I already looked into spring websockets but i dont want to use them. I also dont want to use STOMP.
I can understand ... :-)
Yes, I agree with this. While Spring documents well how to configure your spring application to accept websocket connections (server side), for making websocket connections as a client it doesnt do so well, and it almost religiously pushes its STOMP api. However, its still possible to use the Spring Websocket API without STOMP by using a WebSocketConnectionManager or the StandardWebSocketClient class directly
3

If you want to use java websocket API in a Spring Boot application you have to add this to spring configuration

@Configuration
public class WebSocketConfig {

    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

}

Here is a full article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.