SpringBoot学习笔记11-Web-Reactive Web应用

本文介绍了Spring Boot如何支持WebFlux响应式Web应用程序,包括自动配置、HTTP编解码器、静态资源处理、模板引擎、错误处理和自定义错误页面。Spring WebFlux是Spring 5.0引入的异步非阻塞框架,提供了功能性和基于注解的编程模型。同时,文章讨论了嵌入式响应式服务器支持和资源配置。
摘要由CSDN通过智能技术生成

以spring官方文档为基础,官方地址:Spring Boot_Web

Spring Boot非常适合web应用程序的开发。可以使用嵌入的Tomcat、Jetty、Undertow或Netty创建一个自包含的HTTP服务器。大多数web应用程序使用spring-boot-starter-web模块来快速启动和运行。也可以选择使用spring-boot-starter-webflux模块来构建响应式web应用程序。

Spring Boot通过为Spring Webflux提供自动配置,简化了响应式web应用程序的开发。

1. 关于Spring WebFlux Framework

Spring WebFlux是Spring framework 5.0中引入的一个新的响应式web框架。与Spring MVC不同,它不需要servlet API,是完全异步和非阻塞的,并通过Reactor项目实现了Reactive Streams规范。

Spring WebFlux有两种类型:功能性和基于注解的。基于注解的模型非常接近Spring MVC模型,如下面的例子所示:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class MyRestController {
   

    private final UserRepository userRepository;

    private final CustomerRepository customerRepository;

    public MyRestController(UserRepository userRepository, CustomerRepository customerRepository) {
   
        this.userRepository = userRepository;
        this.customerRepository = customerRepository;
    }

    @GetMapping("/{user}")
    public Mono<User> getUser(@PathVariable Long userId) {
   
        return this.userRepository.findById(userId);
    }

    @GetMapping("/{user}/customers")
    public Flux<Customer> getUserCustomers(@PathVariable Long userId) {
   
        return this.userRepository.findById
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值