简体   繁体   中英

Use Java Websocket API in Spring Boot application

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

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.

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM