Spring reactive rest service returning ServerResponse object

时间:2017-06-15 09:43:10

标签: spring spring-restcontroller spring-webflux

I'm having issues trying to return ServerResponse object fro a reactive rest service

@RequestMapping(path = "/", method = RequestMethod.GET)
    public Mono<ServerResponse> getPages(@RequestParam(value = "siteId", required = false) String siteId,
            @RequestParam(value = "parentId", required = false) String parentId) throws Exception {

        Flux<MyModel> result = pageModelService.getMyModels(Id, name);

        return ServerResponse.ok().body(result, MyModel.class).switchIfEmpty(ServerResponse.notFound().build());
    }

I'm getting

javax.servlet.ServletException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class org.springframework.web.reactive.function.server.DefaultEntityResponseBuilder$DefaultEntityResponse

here's my pom.xml

<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>
    <artifactId>xxxx</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>xx.xxxx</groupId>
        <artifactId>xxxx-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <jetty.version>9.4.5.v20170502</jetty.version>
        <spring.version>5.0.0.RC2</spring.version>
        <spring-boot.version>2.0.0.M1</spring-boot.version>
        <spring-cloud.version>Finchley.BUILD-SNAPSHOT</spring-cloud.version>
        <servlet-api.version>3.1.0</servlet-api.version>
        <lombok.version>1.16.10</lombok.version>
        <pebble-spring-boot-starter.version>2.3.0</pebble-spring-boot-starter.version>
        <springfox.version>2.6.1</springfox.version>
        <reactor.version>3.1.0.M2</reactor.version>
        <rxjava-reactive-streams.version>1.2.1</rxjava-reactive-streams.version>
        <orika.version>1.5.1</orika.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <!-- Jetty BOM -->
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-bom</artifactId>
                <version>${jetty.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Spring BOM -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Spring Boot versions -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <artifactId>slf4j-api</artifactId>
                <groupId>org.slf4j</groupId>
                <version>1.7.25</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <!-- Spring Jetty (Http Server) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <!-- Spring Webflux (Reactive Spring MVC) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>


        <!-- Spring Config (Centralized configuration) -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!-- Spring Actuator (Management endpoints) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Spring Sleuth (request tracking) -->
        <!-- dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency-->

        <!-- Spring Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <!-- Spring Freemarker Template -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-webmvc</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>


        <!-- Jetty Proxy -->
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-proxy</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-client</artifactId>
        </dependency>


        <!-- Lombok (IDE tool) -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox.version}</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox.version}</version>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>${reactor.version}</version>
        </dependency>
        <dependency>
            <groupId>io.reactivex</groupId>
            <artifactId>rxjava-reactive-streams</artifactId>
            <version>${rxjava-reactive-streams.version}</version>
        </dependency>


        <dependency>
            <groupId>ma.glasnost.orika</groupId>
            <artifactId>orika-core</artifactId>
            <version>${orika.version}</version>

        </dependency>


    </dependencies>


    <repositories>
        <!-- Spring Snapshot -->
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>

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

    <pluginRepositories>
        <!-- Spring Snapshot -->
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>

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

    <build>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <!-- Required: The following will ensure `npm install` is called
                         before anything else during the 'Default Lifecycle' -->
                    <execution>
                        <id>npm install (initialize)</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>initialize</phase>
                        <configuration>
                            <executable>npm</executable>
                            <workingDirectory>src/main/resources/angular</workingDirectory>
                            <arguments>
                                <argument>install</argument>
                            </arguments>
                        </configuration>
                    </execution>

                    <execution>
                        <id>npm build</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <phase>initialize</phase>
                        <configuration>
                            <executable>ng</executable>
                            <workingDirectory>src/main/resources/angular</workingDirectory>
                            <arguments>
                                <argument>build</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>


        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/config</directory>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>static/**</include>
                </includes>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.src.less</exclude>
                    <exclude>**/*.src.js</exclude>
                    <exclude>static/**</exclude>
                    <exclude>angular/**</exclude>
                </excludes>
            </resource>
        </resources>
    </build>
</project>

I tried to add jackson dependencies, but seems it's another issue

any idea would be welcome

2 个答案:

答案 0 :(得分:0)

默认情况下,spring-boot-starter-webflux将使用Reactor Netty作为webflux运行时。

如果您想将Jetty用作目标运行时,只需在spring-boot-starter-reactory-netty依赖项中排除spring-boot-starter-webflux并明确添加spring-boot-starter-jetty

然后简单地将Spring引导声明为父POM。

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

清除其他不使用的依赖项,删除您的dependencyManagement。

检查我的工作样本(但我使用默认的Reactor Netty作为运行时):

Vanllia版本:https://github.com/hantsy/spring-reactive-sample/tree/master/vanilla

Spring Boot版本:https://github.com/hantsy/spring-reactive-sample/tree/master/boot

答案 1 :(得分:0)

来自this的答案:

ServerResponse应该与WebFlux Handlers一起使用,而不与RestControllers一起使用。