8

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.

6
  • You should not need to do anything special. Have you tried running it on your Tomcat? If so, what error(s) are you seeing? Commented Aug 22, 2016 at 18:05
  • I'm not even getting the run on server option when I right click the project. I've added a server. Commented Aug 22, 2016 at 18:10
  • I think you either need to create a WAR and upload it to Tomcat (I usually use the GUI), or create a configuration like this. Commented Aug 22, 2016 at 18:46
  • 1
    You're saying it works fine on embedded Tomcat? Cause I do believe the issue is that in the SpringBootServletInitializer you're mapping the app to /app but with the embedded one it will run on /. And if you look at the error it is trying to connect to localhost:8080/hello but should use localhost:8080/app/hello. Commented Aug 25, 2016 at 17:32
  • 3
    If I understand it correctly your context on standalone tomcat is as follows localhost:8080/opl-ws-webui so I think you should connect to localhost:8080/opl-ws-webui/hello/info instead of localhost:8080/hello/info right? Commented Aug 29, 2016 at 13:19

5 Answers 5

1

If you are using Spring Boot, then you could switch to war packaging instead of jar and change to provided scope of tomcat dependency.

For more details check :

Jar to war

Build config

Serve static content

Sign up to request clarification or add additional context in comments.

2 Comments

My project runs fine on the embedded Tomcat instance, I need to know how to deploy it on a local instance of Tomncat
In your question you have commented that you are not getting "run on server " option. If project packaging is war, you will get that
1

Maybe problem with application context. When you are running the app in your local system it map to context root ( / ). But when you upload it to tomcat the context will be your file name. Ex, if your file name is "myapp" the context will be http://server:8080/myapp though in local system it runs as root app. You can solve this problem by setting your app as root context of tomcat or change the app paths to production context ( in this case myapp). To set ROOT app go to (Ubuntu) :

/var/lib/tomcat7/conf/server.xml

Then add,

<Context path="ROOT" docBase="myapp"> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> </Context> 

and remove other root config if any. Finally restart tomcat server.

Comments

0

I use Apache Tomcat Maven Plugin for this purpose but you are using Gradle. It works perfectly with Tomcat 7 but does not support Tomcat 8. Please, see this [link][1]. http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/

Comments

0

SockJS doesn't honour relative URLs. Find an approach to a related problem here

Comments

0

You can not run the app with IDE itself, you have to make a war file and deploy it in tomcat it will be run. make sure that the port number 8080 not used by any other servers/apps.

Comments