为了账号安全,请及时绑定邮箱和手机立即绑定

route

return RouterFunction.route(RequestPredicates.GET("/person/find/all"),

为啥我的这句话中route报错,

代码:

package com.example.somefrist.config;

import com.example.somefrist.domain.User;
import com.example.somefrist.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;

import java.util.Collection;

/**
 *
 *
 * 路由器函数注释
 */

@Configuration
//类所处的对象是配置对象
public class RouterFunctionConfiguration {

    /**
     * Servlet
     * 请求接口  ServletRequest  或者 HttpServletRequest
     * 响应接口  ServletResponse  后者   HttpServletResponse
     *
     * spring5.0重新定义了服务端的请求和响应接口:
     * 请求接口:ServerRequest
     * 响应接口:ServerResponse
     *
     * 既可以支持Servlet规范,也可以支持自定义,比如:Netty(Web Server)
     *
     *
     * 以本利:
     * 定义GET请求,并且返回所有的用户对象,  URI:/person/find/all
     *
     *
     * Flux是0-N个对象集合
     * Mono是 0-1 个对象集合
     *
     * Reactive 中 Flux 或者  Mono  它是异步处理
     *
     * 集合对象基本上是同步处理(阻塞式)
     *
     * Flux 或者 Mono 都是 Publisher,
     */
    @Bean
    @Autowired
    public RouterFunction<ServerResponse> personFindAll (UserRepository userRepository){




        return RouterFunction.route(RequestPredicates.GET("/person/find/all"),
                request->{

                    Collection<User> users =  userRepository.findAll();

                    //返回所有用户对象


                    //Mono<ServerResponse> response = null;
                    Flux<User> userFlux = Flux.fromIterable(users);
                    //ServerResponse.ok().body(userFlux, User.class);
                    return ServerResponse.ok().body(userFlux, User.class);
                });

    }

}


报错原因

https://img1.sycdn.imooc.com//5b0b7bb00001c14010460078.jpg

正在回答

2 回答

https://img1.sycdn.imooc.com//5b0b7d850001aae608280104.jpg

这是运行报错的截图

0 回复 有任何疑惑可以回复我~

这是maven的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>somefrist</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>somefrist</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</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>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.0-beta4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webflux</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
    </dependencies>

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


</project>


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Spring Boot 2.0深度实践-初遇Spring Boot
  • 参与学习       75471    人
  • 解答问题       212    个

Spring Boot 2.x/Web Flux/多模块化项目实践

进入课程
意见反馈 帮助中心 APP下载
官方微信