I've followed the following Spring Web Sockets guide to create an application using Spring Web Sockets on stomp. https://spring.io/guides/gs/messaging-stomp-websocket/ The application works fine when I run it using the Spring Boot embedded Tomcat. however now I want to deploy it on a local instance of Tomcat 7.
I've modified the Application class to the following,
@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; } The WebSocket Configuration is as follows,
@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(); } } And my build.gradle is as follows,
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' } When I right click the project and run it on server from inside eclipse, it serves the correct index.html at the following url. http://localhost:8080/opl-ws-webui/
However when I press the connect button which is supposed to open up a text input box I get nothing. On checking the developer tools, here's the error message that I get.
sockjs-0.3.4.js:807 GET http://localhost:8080/hello/info 404 (Not Found) Whoops! Lost connection to undefined I've googled the error message and tried implementing the suggestions in the following links, Whoops! Lost connection to undefined - Connection Lost Just After The Connection Established Stomp over socket using sockjs can't connect with Spring 4 WebSocket
And yet I continue to get the same error. I've tried deploying on both Tomcat 7 and 8 and still the same. Really appreciate some help on the issue.
SpringBootServletInitializeryou're mapping the app to/appbut with the embedded one it will run on/. And if you look at the error it is trying to connect tolocalhost:8080/hellobut should uselocalhost:8080/app/hello.