Spring Boot 2 反应式 websocket 与 datarest 冲突 [英] Spring boot 2 reactive web websocket conflict with datarest

查看:57
本文介绍了Spring Boot 2 反应式 websocket 与 datarest 冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring boot 2 创建一个项目,并使用响应式 Web 依赖项使用 websocket.我的应用程序正常工作,直到我添加 datarest 依赖项.在我添加 datarest 依赖应用程序后给

I'm using spring boot 2 to create a project and use websocket using reactive web dependency. My application is worked correctly until I add datarest dependency. after I add datarest dependency application give

' 失败:WebSocket 握手期间出错:意外响应代码:404

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

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

is any way to resolve this conflict?.

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-boot-starter-data-rest brings spring-boot-starter-web as a transitive dependency (so basically Spring MVC). This makes Spring Boot configure your application as a Spring MVC web application.

Spring Data REST 目前不支持 Spring WebFlux(有关更多信息,请参阅此问题).

Spring Data REST does not currently support Spring WebFlux (see this issue for more information on that).

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

Your only choice is to remove the Spring Data REST dependency, as you can't have both Spring MVC and Spring WebFlux in the same Spring Boot application.

这篇关于Spring Boot 2 反应式 websocket 与 datarest 冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆