Spring boot 2 reactive web websocket conflict with datarest

让人想犯罪 __ 提交于 2021-02-07 20:26:20

问题


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

' 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;
}

}


回答1:


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 does not currently support Spring WebFlux (see this issue for more information on that).

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.



来源:https://stackoverflow.com/questions/48414750/spring-boot-2-reactive-web-websocket-conflict-with-datarest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!