Tyrus – 简单的java应用程序 – 找不到实现类

前端之家收集整理的这篇文章主要介绍了Tyrus – 简单的java应用程序 – 找不到实现类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发简单的应用程序来帮助我学习 Java中的WebSocket和Tyrus概念.这是我的服务器端(服务器端点)和我的pom.xml以及我的客户端和pom.xml:

服务器端

@ServerEndpoint(value="/websocket/{client-id}")
    public class MyServerEndpoint {

        private static final LinkedList<Session> clients = new LinkedList<Session>();

        @OnOpen
        public void onOpen(Session session) {
            clients.add(session);

        }
        @OnMessage
        public void onMessage(String message,@PathParam("client-id") String clientId) {
            for (Session client : clients) {
                try {
                    client.getBasicRemote().sendObject(clientId+": "+message);          

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (EncodeException e) {
                    e.printStackTrace();
                }
            }
        }
        @OnClose
        public void onClose(Session peer) {
            clients.remove(peer);
        }
    }

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>TyrusJacpFXServer</groupId>
      <artifactId>TyrusJacpFXServer</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>

      <dependencies>

       <dependency>
        <groupId>javax.websocket</groupId>
        <artifactId>javax.websocket-api</artifactId>
        <scope>compile</scope>
        <version>1.0</version>
     </dependency>

      </dependencies>

      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
              <source>1.7</source>
              <target>1.7</target>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
              <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>

客户端

MyClient.java 

@ClientEndpoint
public class MyClient {
    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected to endpoint: " + session.getBasicRemote());
        try {
            session.getBasicRemote().sendText("Hello");
        } catch (IOException ex) {
        }
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println(message);
    }

    @OnError
    public void onError(Throwable t) {
        t.printStackTrace();
    }
}

App.java

public class App {

    public Session session;

    protected void start()
             {

            WebSocketContainer container = ContainerProvider.getWebSocketContainer();

            String uri = "ws://localhost:8080/TyrusJacpFXClient/websocket/desktop-client";
            System.out.println("Connecting to " + uri);
            try {
                session = container.connectToServer(MyClient.class,URI.create(uri));
            } catch (DeploymentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }             

    }
    public static void main(String args[]){
        App client = new App();
        client.start();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        try {
            do{
                input = br.readLine();
                if(!input.equals("exit"))
                    client.session.getBasicRemote().sendText(input);

            }while(!input.equals("exit"));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TyrusJacpFXClient</groupId>
  <artifactId>TyrusJacpFXClient</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>

  <dependency>
    <groupId>javax.websocket</groupId>
    <artifactId>javax.websocket-api</artifactId>
    <scope>compile</scope>
    <version>1.0</version>
 </dependency>

   <dependency>
    <groupId>org.glassfish.tyrus</groupId>
    <artifactId>tyrus-client</artifactId>
    <version>1.8.3</version>
   </dependency>


      <dependency>
            <groupId>org.glassfish.tyrus</groupId>
            <artifactId>tyrus-container-grizzly</artifactId>
            <version>1.1</version>
        </dependency>


  </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

我收到此错误

Exception in thread "main" java.lang.RuntimeException: Could not find an implementation class.
at javax.websocket.ContainerProvider.getWebSocketContainer(ContainerProvider.java:73)
at org.hwc.App.start(App.java:25)
at org.hwc.App.main(App.java:40)

有人可以帮我看看问题吗?服务器在GlassFish 4.0中运行

解决方法

你有依赖问题.这个:
<dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly</artifactId>
        <version>1.1</version>
  </dependency>

是不正确的.你需要使用[1](基于灰熊的客户端):

<dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-grizzly-client</artifactId>
        <version>1.8.3</version>
  </dependency>

[2](jdk 7 NIO客户端impl)

<dependency>
        <groupId>org.glassfish.tyrus</groupId>
        <artifactId>tyrus-container-jdk-client</artifactId>
        <version>1.8.3</version>
  </dependency>
原文链接:https://www.f2er.com/java/128293.html

猜你在找的Java相关文章