admin管理员组

文章数量:1404557

I'm trying to get the basic websocket working in Spring with Jetty, as covered here.

At the time of the upgrade, runtime is reporting that the provider is null.

java.lang.NullPointerException: Cannot invoke ".eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer.upgrade(.eclipse.jetty.ee10.websocket.server.JettyWebSocketCreator, jakarta.servlet.http.HttpServletRequest, jakarta.servlet.http.HttpServletResponse)" because "container" is null
.springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy.upgrade(JettyRequestUpgradeStrategy.java:117)
...

Please suggest what may be missing. There is a myriad of conflicting spring/jetty/Jakarta/ee10 content with very little examples of plain websocket in Spring.

Do I need to use Customizer to set the handler?

main:

@EnableWebMvc
@SpringBootApplication
public class MyBootApplication...


MyWebSocketConfig class: 

import .springframework.beans.factory.annotation.Autowired;
import .springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import .springframework.context.annotation.Bean;
import .springframework.context.annotation.Configuration;
import .springframework.web.socket.config.annotation.EnableWebSocket;
import .springframework.web.socket.config.annotation.WebSocketConfigurer;
import .springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import .springframework.web.socket.server.jetty.JettyRequestUpgradeStrategy;
import .springframework.web.socket.server.support.DefaultHandshakeHandler;

@Configuration
@EnableWebSocket
public class MyWebSocketConfig implements WebSocketConfigurer {

  @Autowired
  private MyWebSocketHandler myWebSocketHandler;

  @Override
  public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry
        .addHandler(myWebSocketHandler, "/init")
        .setHandshakeHandler(handshakeHandler())
        .setAllowedOrigins("*");
  }

  @Bean
  public DefaultHandshakeHandler handshakeHandler() {

    JettyRequestUpgradeStrategy strategy = new JettyRequestUpgradeStrategy();
    strategy.addWebSocketConfigurer(configurable -> {
       configurable.setInputBufferSize(8192);
       configurable.setIdleTimeout(Duration.ofSeconds(600));
    });
        
    return new DefaultHandshakeHandler(strategy);
  }
}

gradle deps

implementation '.springframework:spring-web:6.2.1'
implementation '.springframework:spring-orm:6.2.1'
implementation '.springframework:spring-aop:6.2.1'
implementation '.springframework:spring-tx:6.2.1'
implementation '.springframework:spring-beans:6.2.1'
implementation '.springframework:spring-core:6.2.1'
implementation '.springframework:spring-jcl:6.2.1'
implementation '.springframework:spring-expression:6.2.1'
implementation '.springframework:spring-jdbc:6.2.1'
implementation '.springframework:spring-webmvc:6.2.1'
implementation '.springframework:spring-context:6.2.1'
implementation '.springframework:spring-websocket:6.2.1'

implementation '.springframework.data:spring-data-jpa:3.3.3'
implementation '.springframework.data:spring-data-commons:3.3.3'

implementation '.springframework.security:spring-security-core:6.3.6'
implementation '.springframework.security:spring-security-web:6.3.6'
implementation '.springframework.security:spring-security-config:6.3.6'

implementation '.springframework.boot:spring-boot-starter-web:3.3.7'
implementation(".springframework.boot:spring-boot-starter-jetty:3.3.7") {
  exclude group: ".springframework.boot", module: "spring-boot-starter-tomcat"
}
implementation '.springframework.boot:spring-boot-starter-data-jpa:3.3.7'
implementation '.springframework.boot:spring-boot-starter-cache:3.3.7'
implementation '.springframework.boot:spring-boot-starter-log4j2:3.3.7'
implementation '.springframework.boot:spring-boot-starter-websocket:3.3.7'

implementation '.springframework.boot:spring-boot-autoconfigure:3.3.7'
implementation '.springframework.boot:spring-boot-actuator-autoconfigure:3.3.7'

Tried a number of different dependencies, results vary from "no JSR-356 compatible provider found..." to this null provider.

本文标签: How to do plain websocket with SpringJettyStack Overflow