国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Spring WebFlux的使用指南

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:老K的JAVA博客 欄目: 編程語(yǔ)言 歡迎投稿:712375056

目錄

      Spring WebFlux是spring5的一部分,它為web應用程序提供反應式編程支持。

      在本教程中,我們將使用RestController和WebClient創(chuàng )建一個(gè)小型響應式REST應用程序。

      我們還將研究如何使用Spring安全保護我們的反應端點(diǎn)。

      Spring-WebFlux框架

      Spring WebFlux在內部使用Project Reactor及其發(fā)布者實(shí)現Flux和Mono。

      新框架支持兩種編程模型:

      • 基于注釋的反應元件
      • 功能路由和處理

      依賴(lài)項

      讓我們從spring boot starter webflux依賴(lài)項開(kāi)始,它包含所有其他必需的依賴(lài)項:

      • spring boot和spring boot starter,用于基本的spring boot應用程序設置
      • spring-webflux框架
      • reactor-core我們需要的反應流,也需要reactor-netty
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-webflux</artifactId>
          <version>2.2.6.RELEASE</version>
      </dependency>

      響應式應用

      我們現在將使用Spring WebFlux構建一個(gè)非常簡(jiǎn)單的REST EmployeeManagement應用程序:

      • 我們將使用一個(gè)簡(jiǎn)單的域模型-帶有id和name字段的Employee
      • 我們將使用RestController構建restapi,以將員工資源作為單個(gè)資源和集合發(fā)布
      • 我們將使用WebClient構建一個(gè)客戶(hù)端來(lái)檢索相同的資源
      • 我們將使用WebFlux和Spring Security創(chuàng )建一個(gè)安全的被動(dòng)端點(diǎn)

      響應式RestController

      springwebflux支持基于注釋的配置,方式與springwebmvc框架相同。

      首先,在服務(wù)器上,我們創(chuàng )建一個(gè)帶注釋的控制器,它發(fā)布員工資源的反應流。

      讓我們創(chuàng )建帶注釋的EmployeeController

      @RestController
      @RequestMapping("/employees")
      public class EmployeeController {
      
          private final EmployeeRepository employeeRepository;
          
          // constructor...
      }

      EmployeeRepository可以是任何支持非阻塞反應流的數據存儲庫。

      單一資源

      讓我們在控制器中創(chuàng )建一個(gè)端點(diǎn),用于發(fā)布單個(gè)員工資源:

      @GetMapping("/{id}")
      private Mono<Employee> getEmployeeById(@PathVariable String id) {
          return employeeRepository.findEmployeeById(id);
      }

      我們在Mono中包裝一個(gè)Employee資源,因為我們最多返回一個(gè)Employee。

      集合資源

      我們還要添加一個(gè)端點(diǎn)來(lái)發(fā)布所有雇員的集合資源:

      @GetMapping
      private Flux<Employee> getAllEmployees() {
          return employeeRepository.findAllEmployees();
      }

      對于集合資源,我們使用類(lèi)型為Employee的流量,因為它是0..n元素的發(fā)布者。

      反應式Web客戶(hù)端

      Spring5中引入的WebClient是一個(gè)支持反應流的非阻塞客戶(hù)端。

      我們可以使用WebClient創(chuàng )建一個(gè)客戶(hù)端,從EmployeeController提供的端點(diǎn)檢索數據。

      讓我們創(chuàng )建一個(gè)簡(jiǎn)單的EmployeeWebClient:

      public class EmployeeWebClient {
      
          WebClient client = WebClient.create("http://localhost:8080");
      
          // ...
      }

      在這里,我們使用工廠(chǎng)方法create創(chuàng )建了一個(gè)WebClient。它會(huì )指向localhost:8080,所以我們可以使用或相對的URL來(lái)調用這個(gè)客戶(hù)端實(shí)例。

      檢索單個(gè)資源

      要從endpoint/employee/{id}檢索Mono類(lèi)型的單個(gè)資源,請執行以下操作:

      Mono<Employee> employeeMono = client.get()
        .uri("/employees/{id}", "1")
        .retrieve()
        .bodyToMono(Employee.class);
      
      employeeMono.subscribe(System.out::println);

      檢索集合資源

      類(lèi)似地,要從endpoint/employees檢索Flux類(lèi)型的集合資源,請執行以下操作:

      Flux<Employee> employeeFlux = client.get()
        .uri("/employees")
        .retrieve()
        .bodyToFlux(Employee.class);
              
      employeeFlux.subscribe(System.out::println);

      Spring WebFlux安全性

      我們可以使用Spring Security來(lái)保護我們的反應端點(diǎn)。

      假設我們在EmployeeController中有一個(gè)新的端點(diǎn)。此端點(diǎn)更新員工詳細信息并發(fā)回更新的員工。

      由于這允許用戶(hù)更改現有員工,因此我們希望僅將此端點(diǎn)限制為管理員角色用戶(hù)。

      讓我們?yōu)镋mployeeController添加一個(gè)新方法:

      @PostMapping("/update")
      private Mono<Employee> updateEmployee(@RequestBody Employee employee) {
          return employeeRepository.updateEmployee(employee);
      }

      現在,為了限制對該方法的訪(fǎng)問(wèn),讓我們創(chuàng )建SecurityConfig并定義一些基于路徑的規則以?xún)H允許管理員用戶(hù):

      @EnableWebFluxSecurity
      public class EmployeeWebSecurityConfig {
      
          // ...
      
          @Bean
          public SecurityWebFilterChain springSecurityFilterChain(
            ServerHttpSecurity http) {
              http.csrf().disable()
                .authorizeExchange()
                .pathMatchers(HttpMethod.POST, "/employees/update").hasRole("ADMIN")
                .pathMatchers("/**").permitAll()
                .and()
                .httpBasic();
              return http.build();
          }
      }

      此配置將限制對/employees/update的訪(fǎng)問(wèn)。因此,只有具有ADMIN角色的用戶(hù)才能訪(fǎng)問(wèn)此端點(diǎn)并更新現有員工。

      最后,注解@EnableWebFluxSecurity添加了一些默認配置的Spring-Security-WebFlux支持。

      結論

      在本文中,我們探討了如何創(chuàng )建和使用springwebflux框架支持的反應式web組件。例如,我們構建了一個(gè)小型的REST應用程序。

      除了Reactive RestController和WebClient之外,WebFlux框架還支持Reactive WebSocket和對應的WebSocketClient,以進(jìn)行套接字樣式的Reactive流。

      最后,在Github上提供了本文中使用的完整源代碼:

      以上就是Spring WebFlux的使用指南的詳細內容,更多關(guān)于Spring WebFlux的使用的資料請關(guān)注腳本之家其它相關(guān)文章!

      免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

      农村熟女大胆露脸自拍| 亚洲女久久久噜噜噜熟女| 70歳の熟女セックス合集| 无码专区视频精品老司机| 狠狠色狠狠色综合久久| 97色精品视频在线观看|