我已按照以下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;
}
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();
}
}
我的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'
}
当我右键单击项目并从eclipse内部在服务器上运行它时,它会在以下url处提供正确的index.html。 http://localhost:8080/opl-ws-webui/
然而,当我按下应该打开文本输入框的连接按钮时,我什么也得不到。在检查开发人员工具时,这是我收到的错误消息。
sockjs-0.3.4.js:807 GET http://localhost:8080/hello/info 404 (Not Found)
Whoops! Lost connection to undefined
我已用Google搜索错误消息并尝试在以下链接中实施建议, Whoops! Lost connection to undefined - Connection Lost Just After The Connection Established Stomp over socket using sockjs can't connect with Spring 4 WebSocket
然而我仍然得到同样的错误。我已经尝试在Tomcat 7和8上进行部署,但仍然是相同的。真的很感激这个问题的一些帮助。
答案 0 :(得分:1)
如果您使用的是Spring Boot,那么您可以切换到war
打包而不是jar,并更改为provided
范围的tomcat依赖。
有关详情,请查看:
答案 1 :(得分:1)
可能是应用程序上下文的问题。在本地系统中运行应用程序时,它会映射到上下文根(/)。但是当你将它上传到tomcat时,上下文将是你的文件名。例如,如果您的文件名是&#34; myapp&#34;上下文将是http://server:8080/myapp,但在本地系统中它以root应用程序运行。您可以通过将应用程序设置为tomcat的根上下文或将应用程序路径更改为生产上下文(在本例中为myapp)来解决此问题。 设置ROOT应用程序转到(Ubuntu):
/var/lib/tomcat7/conf/server.xml
然后添加,
<Context path="ROOT" docBase="myapp">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
并删除其他root配置(如果有)。 最后重启tomcat服务器。
答案 2 :(得分:0)
我为此目的使用Apache Tomcat Maven插件,但您使用的是Gradle。它与Tomcat 7完美配合,但不支持Tomcat 8。 请看这[链接] [1]。 http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/
答案 3 :(得分:0)
SockJS并不尊重相对网址。 找到相关问题的方法here
答案 4 :(得分:0)
你无法使用IDE本身运行应用程序,你必须制作一个war文件并将其部署在tomcat中它将运行。确保端口号8080未被任何其他服务器/应用程序使用。