2017-06-15 73 views
0

我有问题试图返回ServerResponse对象来回活性休息服务春天反应REST服务返回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: 组织.springframework.web.util.NestedServletException:请求 处理失败;嵌套的例外是 java.lang.IllegalArgumentException异常:未找到返回 值类型的转换:类 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> 

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

任何想法会受到欢迎

+0

难道您发布MyModel.java?你有所有领域的获得者/安装者吗? –

+0

MyModel.java是一个简单的2字符串属性,我使用的是lombok,并且安装程序和Getter无处不在 此外,如果我返回Flux 它工作正常 – Seb

+0

@Seb您能否提供您的'pom.xml'文件? – ledniov

回答

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。

检查我的工作示例(但我使用的是默认堆的Netty为运行时):

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

春天启动的版本:https://github.com/hantsy/spring-reactive-sample/tree/master/boot