如何沿着 Websocket 端点实现 Rest 端点
How to implement a Rest endpoint along a Websocket endpoint
我想在 Websocket 端点上公开一个额外的 Rest 端点。
websocket 端点已经实现并且可以工作,但我无法访问 Rest 端点。
知道我在这里遗漏了什么吗?
我希望 运行 localhost:8080/hello
的纯文本结果为 hello
.
顺便说一句,如果重要的话,我正在使用 Quarkus。
这是我的项目结构
│ │ ├── java
│ │ │ └── org
│ │ │ └── company
│ │ │ ├── chat
│ │ │ │ ├── boundary
│ │ │ │ ├── control
│ │ │ │ └── entity
│ │ │ ├── JAXRSConfiguration.java // Rest endpoint ???
│ │ │ └── websockets
│ │ │ └── ChatSocket.java // Websocket server endpoint (works)
│ │ └── resources
│ │ ├── application.properties
│ │ ├── application.properties.example
│ │ └── META-INF
│ │ └── resources
│ │ ├── index.html
JAXRSConfiguration.java
package org.company;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
@ApplicationPath("hello")
public class JAXRSConfiguration extends Application {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
ChatSocket.java
package org.company.websockets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.time.LocalDateTime;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.company.chat.boundary.ChatResource;
import org.company.chat.entity.Chat;
import org.company.chat.entity.ChatGroup;
import org.company.chat.entity.ChatMessage;
import org.company.time.Time;
import org.jboss.logging.Logger;
@ServerEndpoint("/chat/websocket/{username}/{chatRoom}")
@ApplicationScoped
public class ChatSocket {
@Inject
Time time;
@Inject
ChatResource chatResource;
// Chatroom, user, session dictionary
final Dictionary<String, Map<String, Session>> chatRooms = new Hashtable<>();
@OnOpen
public void onOpen(final Session session, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom) {
// ...
}
@OnClose
public void onClose(final Session session, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom)
throws Exception {
// ...
}
@OnError
public void onError(final Session session, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom,
final Throwable throwable) throws Exception {
// ...
}
@OnMessage
public void onMessage(String json, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom)
throws Exception {
// ...
}
private void broadcast(final String event, final String chatRoom) {
// ...
}
解决方案只是添加另一个 @Path
注释。
我在这里更改了 JAXRSConfiguration.java
,如下所示。
如果我调用 localhost:8080/api/hello
端点,现在可以使用。
@ApplicationPath("api") // Changed the basic route to 'api'
@Path("hello") // Added a new path
public class JAXRSConfiguration extends Application {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
我想在 Websocket 端点上公开一个额外的 Rest 端点。
websocket 端点已经实现并且可以工作,但我无法访问 Rest 端点。
知道我在这里遗漏了什么吗?
我希望 运行 localhost:8080/hello
的纯文本结果为 hello
.
顺便说一句,如果重要的话,我正在使用 Quarkus。
这是我的项目结构
│ │ ├── java
│ │ │ └── org
│ │ │ └── company
│ │ │ ├── chat
│ │ │ │ ├── boundary
│ │ │ │ ├── control
│ │ │ │ └── entity
│ │ │ ├── JAXRSConfiguration.java // Rest endpoint ???
│ │ │ └── websockets
│ │ │ └── ChatSocket.java // Websocket server endpoint (works)
│ │ └── resources
│ │ ├── application.properties
│ │ ├── application.properties.example
│ │ └── META-INF
│ │ └── resources
│ │ ├── index.html
JAXRSConfiguration.java
package org.company;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
@ApplicationPath("hello")
public class JAXRSConfiguration extends Application {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}
ChatSocket.java
package org.company.websockets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.time.LocalDateTime;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.company.chat.boundary.ChatResource;
import org.company.chat.entity.Chat;
import org.company.chat.entity.ChatGroup;
import org.company.chat.entity.ChatMessage;
import org.company.time.Time;
import org.jboss.logging.Logger;
@ServerEndpoint("/chat/websocket/{username}/{chatRoom}")
@ApplicationScoped
public class ChatSocket {
@Inject
Time time;
@Inject
ChatResource chatResource;
// Chatroom, user, session dictionary
final Dictionary<String, Map<String, Session>> chatRooms = new Hashtable<>();
@OnOpen
public void onOpen(final Session session, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom) {
// ...
}
@OnClose
public void onClose(final Session session, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom)
throws Exception {
// ...
}
@OnError
public void onError(final Session session, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom,
final Throwable throwable) throws Exception {
// ...
}
@OnMessage
public void onMessage(String json, @PathParam("username") final String username, @PathParam("chatRoom") final String chatRoom)
throws Exception {
// ...
}
private void broadcast(final String event, final String chatRoom) {
// ...
}
解决方案只是添加另一个 @Path
注释。
我在这里更改了 JAXRSConfiguration.java
,如下所示。
如果我调用 localhost:8080/api/hello
端点,现在可以使用。
@ApplicationPath("api") // Changed the basic route to 'api'
@Path("hello") // Added a new path
public class JAXRSConfiguration extends Application {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "hello";
}
}