一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot2使用WebFlux函數(shù)式編程的方法

SpringBoot2使用WebFlux函數(shù)式編程的方法

2021-05-24 13:50大老楊 Java教程

這篇文章主要介紹了SpringBoot2使用WebFlux函數(shù)式編程的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文只是簡(jiǎn)單使用springboot2使用webflux的函數(shù)式編程簡(jiǎn)單使用,后續(xù)會(huì)繼續(xù)寫關(guān)于webflux相關(guān)的文章。

最近一直在研究webflux,后續(xù)會(huì)陸續(xù)出一些相關(guān)的文章。

首先看一下srping官網(wǎng)上的一張圖,對(duì)比一下springmvc和spring webflux,如圖:

SpringBoot2使用WebFlux函數(shù)式編程的方法

在查看一下webflux的官方文檔:https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html,webflux提供了函數(shù)式編程,本文簡(jiǎn)單介紹一下webflux函數(shù)式編程簡(jiǎn)單使用。

新建項(xiàng)目

創(chuàng)建一個(gè)項(xiàng)目,pom文件中引入webflux依賴,完整pom文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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.dalaoyang</groupid>
  <artifactid>springboot2_webflux</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>springboot2_webflux</name>
  <description>springboot2_webflux</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.3.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</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-webflux</artifactid>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
 
 
</project>

首先試試引入webflux依賴之后,springmvc方式是否還能使用,新建一個(gè)hellocontroller,完整代碼如下,執(zhí)行后發(fā)現(xiàn),是可以正常執(zhí)行訪問的,這其實(shí)就是我們所說的注解式編程。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.dalaoyang.controller;
 
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
/**
 * @author dalaoyang
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email [email protected]
 * @date 2018/7/30
 */
@restcontroller
public class hellocontroller {
 
  @getmapping("hello")
  public string hello(){
    return "hello this is springwebflux";
  }
}

結(jié)果如圖:

SpringBoot2使用WebFlux函數(shù)式編程的方法

接下來使用函數(shù)式編程,首先查閱一下官方文檔,如圖:

SpringBoot2使用WebFlux函數(shù)式編程的方法

我們需要?jiǎng)?chuàng)建一個(gè)handlerfunction返回值為mono,新建一個(gè)hihandler,里面寫一個(gè)方法hi,完整代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.dalaoyang.handler;
import org.springframework.http.mediatype;
import org.springframework.stereotype.component;
import org.springframework.web.reactive.function.bodyinserters;
import org.springframework.web.reactive.function.server.serverrequest;
import org.springframework.web.reactive.function.server.serverresponse;
import reactor.core.publisher.mono;
 
 
/**
 * @author dalaoyang
 * @project springboot_learn
 * @package com.dalaoyang.handler
 * @email [email protected]
 * @date 2018/7/30
 */
@component
public class hihandler {
 
 
  public mono<serverresponse> hi(serverrequest request) {
    return serverresponse.ok().contenttype(mediatype.application_json)
        .body(bodyinserters.fromobject("hi , this is springwebflux"));
  }
}

其中serverresponse是相應(yīng)的封裝對(duì)象,下面是它的源碼,其中包含了響應(yīng)狀態(tài),響應(yīng)頭等等,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.springframework.web.reactive.function.server;
 
import java.net.uri;
import java.time.zoneddatetime;
import java.util.list;
import java.util.map;
import java.util.set;
import java.util.function.bifunction;
import java.util.function.consumer;
import org.reactivestreams.publisher;
import org.springframework.core.parameterizedtypereference;
import org.springframework.http.cachecontrol;
import org.springframework.http.httpheaders;
import org.springframework.http.httpmethod;
import org.springframework.http.httpstatus;
import org.springframework.http.mediatype;
import org.springframework.http.responsecookie;
import org.springframework.http.codec.httpmessagewriter;
import org.springframework.http.server.reactive.serverhttpresponse;
import org.springframework.util.multivaluemap;
import org.springframework.web.reactive.function.bodyinserter;
import org.springframework.web.reactive.result.view.viewresolver;
import org.springframework.web.server.serverwebexchange;
import reactor.core.publisher.mono;
 
public interface serverresponse {
  httpstatus statuscode();
 
  httpheaders headers();
 
  multivaluemap<string, responsecookie> cookies();
 
  mono<void> writeto(serverwebexchange var1, serverresponse.context var2);
 
  static serverresponse.bodybuilder from(serverresponse other) {
    return new defaultserverresponsebuilder(other);
  }
 
  static serverresponse.bodybuilder status(httpstatus status) {
    return new defaultserverresponsebuilder(status);
  }
 
  static serverresponse.bodybuilder status(int status) {
    return new defaultserverresponsebuilder(status);
  }
 
  static serverresponse.bodybuilder ok() {
    return status(httpstatus.ok);
  }
 
  static serverresponse.bodybuilder created(uri location) {
    serverresponse.bodybuilder builder = status(httpstatus.created);
    return (serverresponse.bodybuilder)builder.location(location);
  }
 
  static serverresponse.bodybuilder accepted() {
    return status(httpstatus.accepted);
  }
 
  static serverresponse.headersbuilder<?> nocontent() {
    return status(httpstatus.no_content);
  }
 
  static serverresponse.bodybuilder seeother(uri location) {
    serverresponse.bodybuilder builder = status(httpstatus.see_other);
    return (serverresponse.bodybuilder)builder.location(location);
  }
 
  static serverresponse.bodybuilder temporaryredirect(uri location) {
    serverresponse.bodybuilder builder = status(httpstatus.temporary_redirect);
    return (serverresponse.bodybuilder)builder.location(location);
  }
 
  static serverresponse.bodybuilder permanentredirect(uri location) {
    serverresponse.bodybuilder builder = status(httpstatus.permanent_redirect);
    return (serverresponse.bodybuilder)builder.location(location);
  }
 
  static serverresponse.bodybuilder badrequest() {
    return status(httpstatus.bad_request);
  }
 
  static serverresponse.headersbuilder<?> notfound() {
    return status(httpstatus.not_found);
  }
 
  static serverresponse.bodybuilder unprocessableentity() {
    return status(httpstatus.unprocessable_entity);
  }
 
  public interface context {
    list<httpmessagewriter<?>> messagewriters();
 
    list<viewresolver> viewresolvers();
  }
 
  public interface bodybuilder extends serverresponse.headersbuilder<serverresponse.bodybuilder> {
    serverresponse.bodybuilder contentlength(long var1);
 
    serverresponse.bodybuilder contenttype(mediatype var1);
 
    serverresponse.bodybuilder hint(string var1, object var2);
 
    <t, p extends publisher<t>> mono<serverresponse> body(p var1, class<t> var2);
 
    <t, p extends publisher<t>> mono<serverresponse> body(p var1, parameterizedtypereference<t> var2);
 
    mono<serverresponse> syncbody(object var1);
 
    mono<serverresponse> body(bodyinserter<?, ? super serverhttpresponse> var1);
 
    mono<serverresponse> render(string var1, object... var2);
 
    mono<serverresponse> render(string var1, map<string, ?> var2);
  }
 
  public interface headersbuilder<b extends serverresponse.headersbuilder<b>> {
    b header(string var1, string... var2);
 
    b headers(consumer<httpheaders> var1);
 
    b cookie(responsecookie var1);
 
    b cookies(consumer<multivaluemap<string, responsecookie>> var1);
 
    b allow(httpmethod... var1);
 
    b allow(set<httpmethod> var1);
 
    b etag(string var1);
 
    b lastmodified(zoneddatetime var1);
 
    b location(uri var1);
 
    b cachecontrol(cachecontrol var1);
 
    b varyby(string... var1);
 
    mono<serverresponse> build();
 
    mono<serverresponse> build(publisher<void> var1);
 
    mono<serverresponse> build(bifunction<serverwebexchange, serverresponse.context, mono<void>> var1);
  }
}

在回過頭了看上面官方文檔的圖片,還需要配置一個(gè)路由來類似@requestmapping的功能,通過routerfunctions.route(requestpredicate, handlerfunction)提供了一個(gè)路由器函數(shù)默認(rèn)實(shí)現(xiàn),新建一個(gè)hirouter,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.dalaoyang.router;
 
import com.dalaoyang.handler.hihandler;
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;
/**
 * @author dalaoyang
 * @project springboot_learn
 * @package com.dalaoyang.router
 * @email [email protected]
 * @date 2018/7/30
 */
@configuration
public class hirouter {
  @bean
  public routerfunction<serverresponse> routecity(hihandler hihandler) {
    return routerfunctions
        .route(requestpredicates.get("/hi")
                .and(requestpredicates.accept(mediatype.application_json)),
            hihandler::hi);
  }
}

啟動(dòng)項(xiàng)目,通過控制臺(tái)可以看到,兩種方式的映射都被打印出來了,如圖所示:

SpringBoot2使用WebFlux函數(shù)式編程的方法

在瀏覽器訪問,http://localhost:8080/hi,結(jié)果如圖所示:

SpringBoot2使用WebFlux函數(shù)式編程的方法

源碼下載 :大老楊碼云

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/qq_33257527/article/details/81300802

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品一区二区三区自拍图片区 | 不知火舞被c视频在线播放 不卡一区二区三区卡 | 亚洲精品AV无码喷奶水糖心 | 色帝国亚洲欧美在线蜜汁tv | 粗了大了 整进去好爽视频 刺激一区仑乱 | 24adc年龄18岁欢迎大驾光临 | 国产一线天 | 日韩欧一级毛片在线播无遮挡 | 亚洲国产成人精品激情 | 青青国产在线观看 | 日韩天堂在线 | 国产第一页无线好源 | 波多野给衣一区二区三区 | 黄篇网站在线观看 | 无套白浆 | 国产va免费精品高清在线观看 | 婷婷草| 国产青青操 | 亚洲+欧美+国产+综合 | 逼里逼里香| 波多野结衣在线免费观看 | 色老板最新网站视频地址 | 欧美日韩一二三区免费视频观看 | 国产亚洲精品视频中文字幕 | 国产精品久久久久久久久 | 国产亚洲欧美在线中文bt天堂网 | 久久久免费观看 | 欧美一级欧美一级高清 | yellow片在线观看 | 免费黄色片在线观看 | 好男人资源免费播放 | 特大黑人娇小亚洲女mp4 | 精品伊人| 亚洲欧美一区二区久久 | 国产成人刺激视频在线观看 | 国产成人8x视频一区二区 | 12345国产精品高清在线 | 极品美女a∨片在线看 | 国产免费美女视频 | 手机av| 成人18视频在线观看 |