在外部Tomcat上部署项目

8 java spring tomcat spring-boot spring-websocket

我按照以下Spring Web套接字指南在stomp上使用Spring Web Sockets创建了一个应用程序. https://spring.io/guides/gs/messaging-stomp-websocket/ 当我使用Spring Boot嵌入式Tomcat运行它时,应用程序正常工作.但是现在我想将它部署在Tomcat 7的本地实例上.

我已将Application类修改为以下内容,

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(applicationClass, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(applicationClass);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.scan(WebSocketConfig.class.getPackage().getName());

        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(context));

        appServlet.setLoadOnStartup(1);
        Set<String> mappings = appServlet.addMapping("/app");

        if (!mappings.isEmpty()) {
            throw new IllegalStateException("Conflicting mappings found! Terminating. " + mappings);
        }
    }

    private static Class<Application> applicationClass = Application.class;
}
Run Code Online (Sandbox Code Playgroud)

WebSocket配置如下,

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/hello").withSockJS();
    }

}
Run Code Online (Sandbox Code Playgroud)

我的build.gradle如下,

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

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

war {
    baseName = 'opl-ws-webui'
    version =  '0.1.0'
}
repositories {
    jcenter()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

bootRepackage {
    enabled false
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-websocket")
    compile("org.springframework:spring-messaging")
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.8.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.8.1'
    compile files('lib/opla230.jar')
    //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    testCompile("junit:junit")
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}
Run Code Online (Sandbox Code Playgroud)

当我右键单击项目并从eclipse内部在服务器上运行它时,它会在以下url处提供正确的index.html. HTTP://本地主机:8080/OPL-WS-Web用户界面/

但是,当我按下应该打开文本输入框的连接按钮时,我什么也得不到.在检查开发人员工具时,这是我收到的错误消息.

sockjs-0.3.4.js:807 GET http://localhost:8080/hello/info 404 (Not Found)
Whoops! Lost connection to undefined
Run Code Online (Sandbox Code Playgroud)

我已经用Google搜索了错误消息并尝试在以下链接中实施建议, 哎呀!丢失连接到未定义 - 连接建立后连接丢失 使用sockjs无法连接Spring 4 WebSocket

但我仍然得到同样的错误.我已经尝试在Tomcat 7和8上进行部署,但仍然是相同的.真的很感激这个问题的一些帮助.

akg*_*aur 1

如果您使用 Spring Boot,那么您可以切换到war打包而不是 jar 并更改providedtomcat 依赖项的范围。

欲了解更多详情,请检查:

战争之罐

构建配置

提供静态内容