2015-01-17 3 views
5

봄 websocket 끝점을 만들려고합니다. 그러나 클라이언트 측에서 연결을 시도 할 때마다 404가 표시됩니다.스프링 websocket 얻는 404 찾을 수 없습니다.

@ServerEndpoint(value = "/websocket") 

이 좋은 Java 구현도 있습니다. 아래는 작동하지 않는 스프링 구현 코드입니다. 여기서 내가 뭘 잘못하고 있니? 아래

package com.tlab.endpoint; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.socket.WebSocketHandler; 
import org.springframework.web.socket.config.annotation.EnableWebSocket; 
import org.springframework.web.socket.config.annotation.WebSocketConfigurer; 
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; 

@Configuration 
@EnableWebSocket 
public class WebSocketConfig implements WebSocketConfigurer { 

    @Override 
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { 
     registry.addHandler(socketHandler(),"/socket");; 
    } 

    @Bean 
    public WebSocketHandler socketHandler() { 
     return new SocketHandler(); 
    } 

} 

내가 모든/휴식/소켓 핸들러

var websocket = new WebSocket("ws://localhost:8080/tsim/socket"); 
var websocket = new WebSocket("ws://localhost:8080/tsim/socket"); 
var websocket = new WebSocket("ws://localhost:8080/tsim/rest/socket"); 

에/소켓 가능한 조합

을 diff를 시도 클라이언트 측 코드에서 핸들러

package com.tlab.endpoint; 

import org.springframework.web.socket.TextMessage; 
import org.springframework.web.socket.WebSocketSession; 
import org.springframework.web.socket.handler.TextWebSocketHandler; 


public class SocketHandler extends TextWebSocketHandler { 

    @Override 
    public void handleTextMessage(WebSocketSession session, TextMessage message) { 

    } 

} 

입니다 처리기 내

var websocket = new WebSocket("ws://localhost:8080/tsim/rest/socket"); 

여기서 tsim은 나의 컨텍스트 루트입니다. 모든 것은 아래의 오류를 던졌습니다

WebSocket connection to 'ws://localhost:8080/tsim/socket' failed: Error during WebSocket handshake: Unexpected response code: 404. 

컴파일 및 배포 오류가 발생하지 않았습니다. 내가 바람둥이 8.0을 사용하고 아래 디스패처 서블릿 구성입니다.

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:int="http://www.springframework.org/schema/integration" 
     xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp" 
     xmlns:batch="http://www.springframework.org/schema/batch" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xmlns:websocket="http://www.springframework.org/schema/websocket" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
      http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd 
      http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd 
      http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd 
      http://www.springframework.org/schema/websocket 
     http://www.springframework.org/schema/websocket/spring-websocket.xsd" 
     > 

    <import resource="classpath*:web-db-spring.xml"/> 

    <context:component-scan base-package="com.traderslab.nseb,com.tlab.endpoint"/> 

    <aop:aspectj-autoproxy/> 

    <mvc:annotation-driven> 
     <mvc:message-converters> 
      <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
       <property name="prefixJson" value="false"/> 
       <property name="supportedMediaTypes" value="application/json"/> 
       <property name="objectMapper"> 
        <bean class="com.fasterxml.jackson.databind.ObjectMapper"> 
         <property name="serializationInclusion" value="NON_NULL"/> 
        </bean> 
       </property> 
      </bean> 
      <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/> 
     </mvc:message-converters> 
    </mvc:annotation-driven> 

    <mvc:default-servlet-handler/> 

</beans> 

아래 내 web.xml을

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/classes/dispatcher-servlet.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/rest/</url-pattern> 
</servlet-mapping> 
+0

당신은'WS를 보여 'ws : // localhost : 8080/TradeSim/socket'을 보여줍니다. 그것은'tsim' 또는'TradeSim'입니까? – Bogdan

+0

나는 다르게 해봤 다. 그 부분을 tsim이나 TradeSim으로 가져 가려했다. 그 혼란을 피하기 위해서 나는 그것을 편집 할 것이다. – anandaravindan

답변

2

마침내 자신 해답을 찾을 수 있습니다. Dispatcher 서블릿이 정상적인 http 요청으로 처리하므로 @Controller 주석을 WebSocketConfig 클래스에 추가해야합니다.

@Configuration 
@EnableWebSocket 
@Controller 
public class WebSocketConfig implements WebSocketConfigurer 
+0

WebSocketConfig에 @Controller를 추가해도 스프링 부트를 사용하고 있는데 같은 문제가있다. 도와 줘. 나는 놀랍게도 403을 얻고있다. 도울 수 있니? – Greyshack

+0

수정 프로그램을 찾았습니다. 브라우저에서 websocket을 테스트하려면 registry.addHandler (socketHandler(), "/ socket")를 추가해야합니다. setAllowedOrigins ("*"); – Greyshack

0

제 경우, 디스패처 서블릿 매핑에 문제가 있습니다. 나는 단지 * .mvc 매핑을했고, 웹 소켓은하지 않고, 그래서 나는 변경했다 : // localhost를 : 8080/침/당신의 예에서 socket`하지만 오류 메시지

<servlet-mapping> 
    <servlet-name>dispatcherServlet</servlet-name> 
    <url-pattern>*.mvc</url-pattern> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 
관련 문제