如何将Spring Webflux Websocket路由设置为注解?

huangapple go评论78阅读模式
英文:

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 = &quot;/getEcho&quot;)
public Mono&lt;String&gt; getEcho() {
return Mono.just(&quot;echo&quot;);
}
}
public class EchoHandler implements WebSocketHandler {
@Override
public Mono&lt;Void&gt; handle(WebSocketSession session) {
return session.send( session.receive().map(msg -&gt; &quot;RECEIVED ON SERVER :: &quot; + msg.getPayloadAsText()).map(session::textMessage));
}
}
@Configuration
public class EchoConfiguration {
@Bean
public EchoHandler echoHandler() {
return new EchoHandler();
}
@Bean
public HandlerMapping handlerMapping() {
Map&lt;String, WebSocketHandler&gt; map = new HashMap&lt;&gt;();
map.put(&quot;/echo&quot;, 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());
}
}
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.4.RELEASE&lt;/version&gt;
&lt;relativePath /&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;

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&lt;String, WebSocketHandler&gt; map = new HashMap&lt;&gt;();
map.put(&quot;/echo&quot;, 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.

huangapple
  • 本文由 发表于 2020年10月27日 03:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64543596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定