Manu
Manu

Reputation: 599

Using a WebSocket API with Struts 2

I have a Struts 2 web application running on Tomcat 7.0.43 that uses REST and Convention plugin to map all requests. Struts tries to map all requests by itself.

JSR 356 defines server end points using annotations like

@ServerEndpoint(value = "/websocket/chat")

Now, when the broswer tries to connect to ws:/127.0.0.1:8080/websocket/chat, the request fails because the Struts mapper intercepts the request.

Is there anything I can specify in the XML files, such as that the request reaches the right place?

EDIT:

As suggested, I added

<constant name="struts.action.excludePattern" value="/websocket.*?" />

to my Struts configuration, after which the URL /websocket/chat started reaching a 404 error.

Later, I learnt that I need to configure a ServerApplicationConfig implementation. After doing that the websocket starts working fine, but the rest of my application fails to load giving an error:

SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

Here is my class:

public class Socket implements ServerApplicationConfig {

    @Override
    public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned) {

        Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();

        return result;
    }

    @Override
    public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {

        Set<Class<?>> results = new HashSet<Class<?>>();
        for (Class<?> clazz : scanned) {
            results.add(clazz);
        }
        return results;
    }
}

How can I get everything to work together in Harmony?

Note: I am using Struts Spring Plugin for Dependency Injection of Spring Security.

Upvotes: 2

Views: 6092

Answers (1)

Roman C
Roman C

Reputation: 1

You can configure the Struts filter to exclude some URLs via regex pattern. You should add the constant in the struts.xml

<constant name="struts.action.excludePattern" value="^ws://.+$"/>

Use Websocket API for server endpoints.

Upvotes: 6

Related Questions