引起:javax.websocket.DeploymentException:路径[websocket]无效

时间:2016-07-17 03:04:27

标签: tomcat java-ee java-websocket

 **16-Jul-2016 15:50:39.774 SEVERE [localhost-startStop-1]  
 org.apache.catalina.core.StandardContext.startInternal Error during ServletContainerInitializer processing
    javax.servlet.ServletException: javax.websocket.DeploymentException: The path [websocket] is not valid.
        at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:123)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5292)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
        at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1092)
        at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1834)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: javax.websocket.DeploymentException: The path [websocket] is not valid.
        at org.apache.tomcat.websocket.server.UriTemplate.<init>(UriTemplate.java:48)
        at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:200)
        at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:279)
        at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:120)
    ... 12 more**

websocket(java)的代码:

**import java.io.IOException;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.OnClose;
import javax.websocket.Session; 
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;
@ServerEndpoint("webDemo/chat")
public class ChatEntpoint{
    private static final String GUEST_PREFIX = "访客";
    private static final AtomicInteger connectionIds = new AtomicInteger(0);
    private static final Set<ChatEntpoint> clientSet = new      CopyOnWriteArraySet<>();
    private final String nickname;
    private Session session;
    public ChatEntpoint(){
        nickname = GUEST_PREFIX + connectionIds.getAndIncrement();
    }
    @OnOpen
    public void start(Session session){
        this.session = session;
        clientSet.add(this);
        String message = String.format("【%s %s】",nickname,"加入了聊天室!");
        broadcast(message);
    }
    @OnClose
    public void end(){
        clientSet.remove(this);
        String message = String.format("【%s %s】",nickname,"离开了聊天室!");
        broadcast(message);
    }
    @OnMessage
    public void incoming(String message){
        String filteredMessage = String.format("【%s %s】",nickname,filter(message));
        broadcast(filteredMessage);
    }
    @OnError
    public void OnError(Throwable t) throws Throwable{
         System.out.println("WebSocket服务器端错误" + t);
    }
    private static void broadcast(String msg){
        for(ChatEntpoint client:clientSet){
            try{
                synchronized(client){
                    client.session.getBasicRemote().sendText(msg);
                }
            }
            catch(IOException e){
                System.out.println("聊天错误,向客户端"+client+"发送消息出现错误!");
                clientSet.remove(client);
                try{
                    client.session.close();
                }
                catch(IOException e1){}
                String message = String.format("【%s %s】",client.nickname,"已经被断开了连接!");
                broadcast(message);             
            }
        } 
    }
    private static String filter(String message){
        if(message == null){
            return null;
        }
        char content[] = new char[message.length()];
        message.getChars(0,message.length(),content,0);
        StringBuilder result = new StringBuilder(content.length + 50);
        for(int i=0;i<content.length;i++){
            switch(content[i]){
                case '<':
                    result.append("&lt;");
                    break;
                case '>':
                    result.append("&gt;");
                    break;
                case '&':
                    result.append("&amp;");
                    break;
                case '"':
                    result.append("&quot;");
                    break;
                default:
                    result.append(content[i]);
             }   
        }
        return (result.toString());
    }
 }**

我尝试构建一个实时同步网站。我已经下载了tomcat 8.0,并在java中构建了websocket服务器。当运行Web时,浏览器出错404.日志写成:

我在网上搜索了很长时间。但没有用。请帮忙或尝试提供一些如何存档的想法。

电脑配置: windows-7 64 i5-3230M RAM 4G

3 个答案:

答案 0 :(得分:0)

问题出现在@ServerEndpoint

的值上
@ServerEndpoint("webDemo/chat")

该值应固定为另一个值,而不是html的名称。

答案 1 :(得分:0)

问题出在// In header file namespace testing{ extern std::map<std::string,int> example; } 的值上。您错过了@ServerEndpoint之前的价值。

你应该用这个:

/

答案 2 :(得分:0)

你应该用这个:

@Serverendpoint("webDemo/{chat}")