Jersey hides Spring framework and SockJS

Viewed 300

We're trying to expose sockjs endpoints with Spring Framework WebSocket support.

This is the configuration on the server side where Jersey is managing the routes:

@Configuration
@EnableWebSocket
public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/sockjs").withSockJS()
                .setStreamBytesLimit(512 * 1024)
                .setHttpMessageCacheSize(1000)
                .setDisconnectDelay(30 * 1000);
    }
}

The problem is that we can't access /sockjs, the client code is:

List<Transport> transports = new ArrayList<>(2);
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
transports.add(new RestTemplateXhrTransport());

SockJsClient sockJsClient = new SockJsClient(transports);
sockJsClient.doHandshake(new MyHandler(), "ws://localhost:8080/sockjs");

(the code is from spring websockets tutorial)

Other resources in the same package would've configured under root/api/server, even though, not /sockjs nor /root/api/server/sockjs are accessible.

1 Answers

I know that this is quite ancient question, but lately I've bumped into the same problem and I've spent half a day to find a solution. As I've found one I'd like to share it, at least for future myself.

The problem was that I had only Jersey's servlet definition inside my WEB-INF.xml. The solution was to add Spring's DispatcherServlet and its mapping to the /* route.

So basically it looks like

<servlet>
  <servlet-name>spring</servlet-mapping>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-servlet.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  <async-supported>true</async-supported>
</servlet>
<servlet-mapping>
  <servlet-name>spring</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>
Related