gpt4 book ai didi

spring-boot - Spring boot 2 响应式(Reactive) web websocket 与 datarest 冲突

转载 作者:行者123 更新时间:2023-12-02 01:04:50 25 4
gpt4 key购买 nike

我正在使用 spring boot 2 创建一个项目,并使用 websocket 和响应式(Reactive)网络依赖。在我添加 datarest 依赖项之前,我的应用程序可以正常工作。在我添加 datarest 依赖应用程序后给出

' failed: Error during WebSocket handshake: Unexpected response code: 404

有什么办法可以解决这个冲突吗?

pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.integration/spring-integration-file -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-file</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

WebSocketConfiguration

@Configuration
public class WebSocketConfiguration {
@Bean
public IntegrationFlow fileFlow(PublishSubscribeChannel channel, @Value("file://${HOME}/Desktop/in") File file) {
FileInboundChannelAdapterSpec in = Files.inboundAdapter(file).autoCreateDirectory(true);

return IntegrationFlows.from(
in,
p -> p.poller(pollerFactory -> {
return pollerFactory.fixedRate(1000);
})
).channel(channel).get();
}

@Bean
@Primary
public PublishSubscribeChannel incomingFilesChannel() {
return new PublishSubscribeChannel();
}

@Bean
public WebSocketHandlerAdapter webSocketHandlerAdapter() {
return new WebSocketHandlerAdapter();
}

@Bean
public WebSocketHandler webSocketHandler(PublishSubscribeChannel channel) {
return session -> {
Map<String, MessageHandler> connections = new ConcurrentHashMap<>();
Flux<WebSocketMessage> publisher = Flux.create((Consumer<FluxSink<WebSocketMessage>>) fluxSink -> {
connections.put(session.getId(), new ForwardingMessageHandler(session, fluxSink));
channel.subscribe(connections.get(session.getId()));
}).doFinally(signalType -> {
channel.unsubscribe(connections.get(session.getId()));
connections.remove(session.getId());
});
return session.send(publisher);
};
}

@Bean
public HandlerMapping handlerMapping(WebSocketHandler webSocketHandler) {
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setOrder(10);
handlerMapping.setUrlMap(Collections.singletonMap("/ws/files", webSocketHandler));
return handlerMapping;
}

最佳答案

spring-boot-starter-data-rest 带来了 spring-boot-starter-web 作为传递依赖(所以基本上是 Spring MVC)。这使得 Spring Boot 将您的应用程序配置为 Spring MVC Web 应用程序。

Spring Data REST 当前不支持 Spring WebFlux (see this issue for more information on that)。

您唯一的选择是删除 Spring Data REST 依赖项,因为您不能在同一个 Spring Boot 应用程序中同时拥有 Spring MVC 和 Spring WebFlux。

关于spring-boot - Spring boot 2 响应式(Reactive) web websocket 与 datarest 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48414750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com