英文:
How to have Spring Webflux Websocket route as annotation?
问题
有关如何在Spring Webflux中使用WebSocket注解的问题,请参考以下内容:
@SpringBootApplication
public class EchoApplication {
public static void main(String[] args) {
SpringApplication.run(EchoApplication.class, args);
}
}
@RestController
public class EchoController {
@GetMapping(value = "/getEcho")
public Mono<String> getEcho() {
return Mono.just("echo");
}
}
@WebSocketMapping("/echo") // 这里是你期望的WebSocketMapping注解
public class EchoHandler implements WebSocketHandler {
@Override
public Mono<Void> handle(WebSocketSession session) {
return session.send(session.receive().map(msg -> "RECEIVED ON SERVER :: " + msg.getPayloadAsText()).map(session::textMessage));
}
}
@Configuration
public class EchoConfiguration {
@Bean
public WebSocketHandlerAdapter handlerAdapter() {
return new WebSocketHandlerAdapter(webSocketService());
}
@Bean
public WebSocketService webSocketService() {
return new HandshakeWebSocketService(new ReactorNettyRequestUpgradeStrategy());
}
}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
在这个示例中,我使用了 @WebSocketMapping("/echo")
这个自定义的注解,你可以根据需要实现这个注解以及相关的处理逻辑。这将帮助你以统一和清晰的方式查看路由。
谢谢!
英文:
Question regarding how to have Spring Webflux Websocket as annotation please.
I am working on a Springboot Webflux Websocket project where it handles BOTH restful api AND a websocket.
To emphasize, it is a Spring Webflux project alone. Not a Springboot starter websocket, not a Springboot rsocket, not a Springboot pub sub.
The project is as follow:
@SpringBootApplication
public class EchoApplication {
public static void main(String[] args) {
SpringApplication.run(EchoApplication.class, args);
}
}
@RestController
public class EchoController {
@GetMapping(value = "/getEcho")
public Mono<String> getEcho() {
return Mono.just("echo");
}
}
public class EchoHandler implements WebSocketHandler {
@Override
public Mono<Void> handle(WebSocketSession session) {
return session.send( session.receive().map(msg -> "RECEIVED ON SERVER :: " + msg.getPayloadAsText()).map(session::textMessage));
}
}
@Configuration
public class EchoConfiguration {
@Bean
public EchoHandler echoHandler() {
return new EchoHandler();
}
@Bean
public HandlerMapping handlerMapping() {
Map<String, WebSocketHandler> map = new HashMap<>();
map.put("/echo", echoHandler());
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(map);
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE);
return mapping;
}
@Bean
public WebSocketHandlerAdapter handlerAdapter() {
return new WebSocketHandlerAdapter(webSocketService());
}
@Bean
public WebSocketService webSocketService() {
return new HandshakeWebSocketService(new ReactorNettyRequestUpgradeStrategy());
}
}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
As you can see, in this project, there is the "traditional" annotation @GetMapping(value = "/getEcho"). Many Spring projects uses this style where it is exposed as annotation (event rscocket, pub/sub, etc...)
How to have:
@Bean
public HandlerMapping handlerMapping() {
Map<String, WebSocketHandler> map = new HashMap<>();
map.put("/echo", echoHandler());
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(map);
as annotation, some kind of @WebsocketMapping("/echo") which will help present an unified and cleaner way to see routes?
Thank you
答案1
得分: 0
来自 Spring Team,目前不支持此功能,也没有计划为 Webflux 支持基于路由的 WebSocket。
英文:
From Spring Team, this is not currently supported, neither have plan to support route based web socket for Webflux.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论