0

我在尝试从反应式休息服务返回 ServerResponse 对象时遇到问题

@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());
    }

我越来越

javax.servlet.ServletException: org.springframework.web.util.NestedServletException: 请求处理失败;嵌套异常是 java.lang.IllegalArgumentException: No converter found for return value of type: class org.springframework.web.reactive.function.server.DefaultEntityResponseBuilder$DefaultEntityResponse

这是我的 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>

我试图添加杰克逊依赖项,但似乎这是另一个问题

任何想法都会受到欢迎

4

2 回答 2

2

这个答案:

ServerResponse 旨在与WebFlux 处理程序一起使用,而不是与 RestControllers 一起使用。

于 2019-04-21T06:32:03.817 回答
0

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

如果您想使用 Jetty 作为目标运行时,只需spring-boot-starter-reactory-nettyspring-boot-starter-webflux依赖项中排除并spring-boot-starter-jetty显式添加。

然后简单地将 Spring boot 声明为父 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

于 2017-08-21T04:04:48.253 回答