• 全国 [切换]
  • 二维码
    筹货展会网

    手机WAP版

    手机也能找商机,信息同步6大终端平台!

    微信小程序

    微信公众号

    当前位置: 首页 » 行业新闻 » 热点新闻 » 正文

    Spring-webflux实战

    放大字体  缩小字体 发布日期:2024-10-10 07:36:34   浏览次数:45  发布人:8af1****  IP:124.223.189***  评论:0
    导读

    1. pom.xml引入以下内容,主要是支持在响应式的环境下对关系型数据库进行访问<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-r2dbc</artifactId></dependency><dependency><groupId>com.github.jasy

    1. pom.xml引入以下内容,主要是支持在响应式的环境下对关系型数据库进行访问

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>com.github.jasync-sql</groupId> <artifactId>jasync-r2dbc-mysql</artifactId> <version>1.1.3</version> </dependency>

    注意: 这里对于SpringBoot的版本似乎是有一些要求的,为了避免遇到无法正常启动的情况,建议使用版本2.3.0-RELEASE版本

    这里附上完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.lazyfennec</groupId> <artifactId>webflux-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>webflux-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.0.RELEASE</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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.springframework.boot</groupId> <artifactId>spring-boot-starter-data-r2dbc</artifactId> </dependency> <dependency> <groupId>com.github.jasync-sql</groupId> <artifactId>jasync-r2dbc-mysql</artifactId> <version>1.1.3</version> </dependency> </dependencies> <!-- 这里很重要,限定了SpringBoot的依赖版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>

    2. 修改application.yml

    spring: r2dbc: url: r2dbcs:mysql://localhost:3306/test username: root password: 123456

    3. 创建实体类User

    package cn.lazyfennec.webfluxdemo.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.data.annotation.Id; import org.springframework.data.relational.core.mapping.Table; @Table("t_user") @Data @AllArgsConstructor @NoArgsConstructor public class User { @Id private Long id; private String name; }

    4. 修改UserController

    package cn.lazyfennec.webfluxdemo.controller; import cn.lazyfennec.webfluxdemo.dao.UserDao; import cn.lazyfennec.webfluxdemo.model.User; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.stream.IntStream; @RestController @Slf4j @AllArgsConstructor public class UserController { private final UserDao userDao; @GetMapping("user") public Flux<User> findAll(){ return userDao.findAll(); } @PostMapping("save") public Mono save(@RequestBody User user){ return userDao.save(user); } @DeleteMapping("user/{id}") public Mono delete(@PathVariable Long id){ return userDao.deleteById(id); } @GetMapping("user/{id}") public Mono findById(@PathVariable Long id){ return userDao.findById(id); } }

    5. 启动类上方增加 @EnableR2dbcRepositories

    package cn.lazyfennec.webfluxdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories; @SpringBootApplication @EnableR2dbcRepositories // 必须新增这个 public class WebfluxDemoApplication { public static void main(String[] args) { SpringApplication.run(WebfluxDemoApplication.class, args); } }

    6. 启动然后访问相关网址测试







    其实除了以上的方式之外,还有另外的方式,即handler 和 router (路由的)方式

    新增以下两个类

    • UserHandler

    package cn.lazyfennec.webfluxdemo.handler; import cn.lazyfennec.webfluxdemo.dao.UserDao; import cn.lazyfennec.webfluxdemo.model.User; import lombok.AllArgsConstructor; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; 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; @Component @AllArgsConstructor public class UserHandler { private final UserDao userDao; //http://localhost:8080/save public Mono<ServerResponse> saveUser(ServerRequest request) { Mono<User> mono = request.bodyToMono(User.class); User user = mono.block(); return ServerResponse.ok().build(userDao.save(user).then()); } public Mono<ServerResponse> deleteUser(ServerRequest request) { Long id = Long.valueOf(request.pathVariable("id")); return ServerResponse.ok().build(userDao.deleteById(id).then()); } public Mono<ServerResponse> getUserbyId(ServerRequest request) { Long id = Long.valueOf(request.pathVariable("id")); Mono<User> mono = userDao.findById(id); return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(mono, User.class); } public Mono<ServerResponse> listUser(ServerRequest serverRequest) { Flux<User> userFlux = userDao.findAll(); return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(userFlux, User.class); } }

    • UserRouter

    package cn.lazyfennec.webfluxdemo.config; import cn.lazyfennec.webfluxdemo.handler.UserHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.server.RequestPredicates; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; @Configuration public class UserRouter { @Bean public RouterFunction<ServerResponse> routUser(UserHandler userHandler) { return RouterFunctions .route(RequestPredicates.GET("/user") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::listUser) .andRoute(RequestPredicates.GET("/user/{id}") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::getUserbyId) .andRoute(RequestPredicates.DELETE("/user/{id}") .and(RequestPredicates.accept(MediaType.APPLICATION_JSON)), userHandler::deleteUser); } }

    • 注释掉UserController

    访问同样的内容




    正常


    Tips

    官方并不建议使用mysql和webflux相结合,主要是事务方面会产生一定的问题。

    关于webflux,在国内其实会使用到的几率并不是很大,所以大致了解一些简单的使用基本就可以了。


    如果觉得有收获就点个赞吧,更多知识,请点击关注查看我的主页信息哦~

     
    (文/匿名(若涉版权问题请联系我们核实发布者) / 非法信息举报 / 删稿)
    打赏
    免责声明
    • 
    本文为昵称为 8af1**** 发布的作品,本文仅代表发布者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,发布者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们154208694@qq.com删除,我们积极做(权利人与发布者之间的调停者)中立处理。郑重说明:不 违规举报 视为放弃权利,本站不承担任何责任!
    有个别老鼠屎以营利为目的遇到侵权情况但不联系本站或自己发布违规信息然后直接向本站索取高额赔偿等情况,本站一概以诈骗报警处理,曾经有1例诈骗分子已经绳之以法,本站本着公平公正的原则,若遇 违规举报 我们100%在3个工作日内处理!
    0相关评论
     

    (c)2008-现在 All Rights Reserved.