spring websocket tomcat was websphere9.0 Multiple Endpoints may not be deployed to the same path

网上说给 去掉 @Configuration , 注释掉 ServerEndpointExporter ,但是  不同环境 不可能 代码乱搞,所以明显是不行的。

下面贴代码:  主要在注册bean时候 ,做了个判断  @Conditional(value=WebSocketCondition.class)

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import org.springframework.web.socket.server.standard.ServerEndpointRegistration;

@Configuration
//@Component
public class EndpointConfig {
    
    @Bean
    public ServerEndpointExporter endpointExporter() {
        return new ServerEndpointExporter();
    }
    /**
     * 在websphere种,无法完成自动注册websocket。
     * 判断WebSocketCondition 是否注册,未注册则执行
     * @return
     */
    @Conditional(value=WebSocketCondition.class)
    @Bean
    public EchoAnnotatedEndpoint echoAnnotatedEndpoint() {
        return new EchoAnnotatedEndpoint(echoService());
    }

    @Bean
    public EchoService echoService() {
        return new DefaultEchoService();
    }
}
WebSocketCondition
import javax.servlet.ServletContext;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class WebSocketCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        XmlWebApplicationContext resourceLoader = (XmlWebApplicationContext) context.getResourceLoader();
        ServletContext servletContext = resourceLoader.getServletContext();
        String serverInfo = servletContext.getServerInfo();
        //tomcat 容器  ,这里可以根据实际业务,自己判断
        if(serverInfo != null && serverInfo.contains("Tomcat")) {
            return false;
        }
        return true;
    }

}

 OK, 问题解决, 说白了,就是bean注册了两次而已

原文地址:https://www.cnblogs.com/bignew/p/13552752.html