由于Tomcat ServerContainer,使用Spring-Websocket的ClassCastException

时间:2016-06-02 14:27:48

标签: spring tomcat gradle websocket spring-websocket

在尝试使用SockJS连接到Spring Websocket时,我收到错误

org.springframework.web.util.NestedServletException: Request processing failed; 
nested exception is org.springframework.web.socket.sockjs.SockJsException: 
Uncaught failure in SockJS request, uri=http://localhost:8083//subscribe/info/771/nay07fet/websocket; 
nested exception is org.springframework.web.socket.sockjs.SockJsTransportFailureException:
WebSocket handshake failure; nested exception is java.lang.ClassCastException: 
org.eclipse.jetty.websocket.jsr356.server.ServerContainer cannot be cast 
to org.apache.tomcat.websocket.server.WsServerContainer

从网上我发现我会在Classpath中禁用Tomcat来克服它,但无论我如何尝试,我都无法摆脱Tomcat。

如何从任何项目中可靠地排除tomcat?请注意,我在几个地方找到了它。

我当前的依赖树在这里:http://pastebin.com/pH1iQejd 它包含一些tomcat。 我需要摆脱可能导致类型混乱的一切。

这是我的gradle.build(我尝试了一些exlcudes但没有成功):

group 'com.company'
if (!hasProperty("buildNumber")) {
    version "1.0.0-SNAPSHOT"
} else {
    version "1.0.${buildNumber}-SNAPSHOT"
}



buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2"
        }


    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
        classpath("gradle.plugin.com.boxfuse.client:flyway-release:4.0.1")
        classpath fileTree(dir: 'libs', include: '*.jar')
        classpath fileTree(dir: 'src/main/resources/db/migration', include: '*.sql')
    }
}


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven {
        url "https://plugins.gradle.org/m2"
    }

}


dependencies {
    // tag::jetty[]
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
        exclude module: 'spring-boot-starter-logging'
    }
    compile("org.springframework.boot:spring-boot-starter-jetty") {
        exclude module: "spring-boot-starter-tomcat"
        exclude module: 'spring-boot-starter-logging'
    }
    // end::jetty[]
    // tag::actuator[]
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-data-jpa"){
        exclude module: "spring-boot-starter-tomcat"
        exclude module: 'spring-boot-starter-logging'
    }
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-websocket")
    // http://mvnrepository.com/artifact/org.springframework.security/spring-security-core
    compile group: 'org.springframework.security', name: 'spring-security-core', version: '4.1.0.RELEASE'

    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.5'
    compile group: 'com.rabbitmq', name: 'amqp-client', version: '3.5.6'
    compile group: 'commons-configuration', name: 'commons-configuration', version: '1.9'
    compile group: 'com.google.guava', name: 'guava', version: '18.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
    compile("org.springframework:spring-messaging")
    compile 'org.springframework.amqp:spring-rabbit:1.5.5.RELEASE'
    compile("com.h2database:h2")
    compile 'org.quartz-scheduler:quartz:2.2.1'
    compile group: 'joda-time', name: 'joda-time', version: '2.3'
    //compile("com.npspot:jtransit-light:1.0.6")
    compile("io.fastjson:boon:0.33")
    compile fileTree(dir: 'libs', include: '*.jar')
    // end::actuator[]
    testCompile group: 'junit', name: 'junit', version: '4.11'
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

configurations {
    compile.exclude module: "spring-boot-starter-tomcat"
    compile.exclude module: 'spring-boot-starter-logging'
    runtime.exclude module: "spring-boot-starter-tomcat"
    runtime.exclude module: 'spring-boot-starter-logging'
}

test {
    reports {
        junitXml.enabled = true
        html.enabled = false
    }
}

springBoot {
    executable = true
}

1 个答案:

答案 0 :(得分:0)

我也遇到了这个问题。我通过将exclude子句添加到websocket依赖项来解决它,即:

compile("org.springframework.boot:spring-boot-starter-websocket") {
    exclude module: "spring-boot-starter-tomcat"
}

在此之后,我意识到除了所有其他的东西之外没有必要;它仅在starter-webstarter-websocket上有必要。