当前springboot版本:2.1.18.RELEASE
1.声明
当前内容主要用于本人学习和使用Spring Boot WebFlux这个东西,基本来源SpringBoot官方文档
2.基本的pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.18.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
3.入口类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
/**
* @author admin
* @createTime 2021-02-27 08:10:07
* @description 当前内容主要用于学习和测试Spring WebFlux
* 个人感觉当前的WebFlux使用方式和当前SpringMVC的方式完全一致,还没有分辨出其中的区别
*/
@SpringBootApplication
public class SpringBootWebFluxApp {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplicationBuilder(SpringBootWebFluxApp.class).build(args);
springApplication.setWebApplicationType(WebApplicationType.REACTIVE);
springApplication.run();
}
}
4.基于配置类的路由
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import com.hy.springboot.webflux.entity.User;
import static org.springframework.web.reactive.function.BodyInserters.fromObject;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
@Configuration
@EnableWebFlux
public class RoutingConfiguration {
// 这个就是专门的路由控制器,也可以使用MVC样式实现
@Bean
public RouterFunction<ServerResponse> monoRouterFunction(UserHandler userHandler) {
return route(GET("/{user}").and(accept(MediaType.APPLICATION_JSON)), userHandler::getUser)
.andRoute(GET("/{user}/customers").and(accept(MediaType.APPLICATION_JSON)), userHandler::getUserCustomers)
.andRoute(DELETE("/{user}").and(accept(MediaType.APPLICATION_JSON)), userHandler::deleteUser);
}
@Component
public class UserHandler {
public Mono<ServerResponse> getUser(ServerRequest request) {
Flux<User> fUser = request.bodyToFlux(User.class);
System.out.println("调用当前的getUser方法....");
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(fUser, User.class);
}
public Mono<ServerResponse> getUserCustomers(ServerRequest request) {
System.out.println("调用当前的getUserCustomers方法....");
return ServerResponse.ok().build();
// ...
}
public Mono<ServerResponse> deleteUser(ServerRequest request) {
System.out.println("调用当前的deleteUser方法....");
return ServerResponse.ok().build();
// ...
}
}
}
5.基于Controller的路由(Spring MVC路由控制)
上面的配置路由中的@Bean方法需要全部注释掉
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import com.hy.springboot.webflux.entity.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author admin
* @createTime 2021-02-27 08:36:53
* @description 专门用于处理User的实体类
*
*/
@RestController
public class UserController {
/**
*
* @author admin
* @createTime 2021-02-27 08:49:19
* @description 通过postman测试成功
* @param id
* @return
*/
@GetMapping("/{id}")
public User getUser(@PathVariable Integer id) {
System.out.println("调用当前的getUser方法....");
return new User(id);
}
@GetMapping("/{id}/customers")
public Mono<ServerResponse> getUserCustomers(@PathVariable Integer id) {
System.out.println("调用当前的getUserCustomers方法....");
return ServerResponse.ok().build();
// ...
}
@DeleteMapping("/{id}")
public Mono<ServerResponse> deleteUser(@PathVariable Integer id) {
System.out.println("调用当前的deleteUser方法....");
return ServerResponse.ok().build();
// ...
}
}
其中User类
public class User {
private Integer id;
private Date now;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Date getNow() {
return now;
}
public void setNow(Date now) {
this.now = now;
}
public User(Integer id) {
super();
this.id = id;
this.now = new Date();
}
public User() {
super();
this.now = new Date();
}
}
6.测试
通过测试发现可以访问,但是如果使用MVC路由控制,基本上和SpringMVC一致,没有太大区别
使用配置类方式配置路由,可以按照传递的数据自动包装,但是感觉和SpringMVC方式没有区别