reactor / reactor-core

Non-Blocking Reactive Foundation for the JVM
http://projectreactor.io
Apache License 2.0
5k stars 1.21k forks source link

java.lang.NoSuchMethodError: reactor.core.publisher.Mono.flatMap issues #527

Closed Theov closed 7 years ago

Theov commented 7 years ago

Hello, I'm trying to play with reactor, on a very simple piece of code but I have an issues with it. I generate a spring boot 2.0.0 application on https://start.spring.io/ with the reactive-web dependency

Here's my source code:

Main class and endpoints

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import java.time.Duration;
import java.util.Date;
import java.util.stream.Stream;

@SpringBootApplication
@RestController
public class ReactiveServiceApplication {
    @GetMapping("/events/{id}")
    Mono<Event> eventById(@PathVariable long id){
        return Mono.just(new Event(id , new Date()));
    }

    @GetMapping(value = "/events", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<Event> events(){
        Flux<Event> eventFlux = Flux.fromStream(Stream.generate(() -> new Event(System.currentTimeMillis(), new Date())));
        Flux<Long> durationFlux = Flux.interval(Duration.ofSeconds(1));
        return  Flux.zip( eventFlux, durationFlux).map(Tuple2::getT1);
    }

    public static void main(String[] args) {
        SpringApplication.run(ReactiveServiceApplication.class, args);
    }
}

Event model

package com.example;

import java.util.Date;

public class Event {
    private long id;
    private Date when;

    public Event(long id, Date date) {
        this.id = id;
        this.when = date;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getWhen() {
        return when;
    }

    public void setWhen(Date when) {
        this.when = when;
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>reactive-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>reactive-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

while i'm requesting both of my endpoints i get the following error

java.lang.NoSuchMethodError: reactor.core.publisher.Mono.flatMap(Ljava/util/function/Function;)Lreactor/core/publisher/Mono;
    at org.springframework.web.reactive.DispatcherHandler.handle(DispatcherHandler.java:129) ~[spring-webflux-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.server.handler.WebHandlerDecorator.handle(WebHandlerDecorator.java:49) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.server.handler.FilteringWebHandler.handle(FilteringWebHandler.java:65) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.server.handler.WebHandlerDecorator.handle(WebHandlerDecorator.java:49) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.server.handler.ExceptionHandlingWebHandler.handle(ExceptionHandlingWebHandler.java:95) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.web.server.adapter.HttpWebHandlerAdapter.handle(HttpWebHandlerAdapter.java:78) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.http.server.reactive.ReactorHttpHandlerAdapter.apply(ReactorHttpHandlerAdapter.java:59) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at org.springframework.http.server.reactive.ReactorHttpHandlerAdapter.apply(ReactorHttpHandlerAdapter.java:37) ~[spring-web-5.0.0.BUILD-20170411.164102-101.jar:5.0.0.BUILD-SNAPSHOT]
    at reactor.ipc.netty.channel.ChannelOperations.applyHandler(ChannelOperations.java:377) ~[reactor-netty-0.6.2.RELEASE.jar:0.6.2.RELEASE]
    at reactor.ipc.netty.http.server.HttpServerOperations.onHandlerStart(HttpServerOperations.java:354) ~[reactor-netty-0.6.2.RELEASE.jar:0.6.2.RELEASE]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.9.Final.jar:4.1.9.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403) [netty-common-4.1.9.Final.jar:4.1.9.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:442) [netty-transport-4.1.9.Final.jar:4.1.9.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) [netty-common-4.1.9.Final.jar:4.1.9.Final]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

I try to flush my maven local repository then to update it but error still there. I don't find any similar issues on github or google.

Am I missing something ?

rstoyanchev commented 7 years ago

Very recent change (today) so certainly a stale snapshot issue one way or another. You did try mvn -U? If you open Mono it should have flatMap, if not check when your local snapshots are from and compare to the latest.

Theov commented 7 years ago

Hello, I try with mvn -U. I have 2 flatMap implementation in the Mono.class. But errors still there.

rstoyanchev commented 7 years ago

Sorry should have been more specific. There was a flatMap before but it was returning Flux and now it returns Mono. Also you should be seeing flatMapMany.

Theov commented 7 years ago

I still have the old public final <R> Flux<R> flatMap implementation and no method flatMapMany. I probably miss something with my dependencies management. Thanks for your reply.

john0xff commented 7 years ago

use flatMapMany instead of flatMap