{"id":3121,"date":"2018-09-27T17:31:02","date_gmt":"2018-09-27T09:31:02","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3121"},"modified":"2018-09-28T16:13:57","modified_gmt":"2018-09-28T08:13:57","slug":"spring-boot-webflux-%e5%85%a5%e9%97%a8%e4%b9%8b-hello-world","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3121","title":{"rendered":"Spring Boot WebFlux \u5165\u95e8\u4e4b Hello World"},"content":{"rendered":"<p>\n\tWebFlux \u5728\u56fd\u5916\u4f7f\u7528\u7684\u5df2\u7ecf\u975e\u5e38\u7684\u591a\u4e86\uff0c\u5728\u56fd\u5185\u7528\u7684\u4eba\u8fd8\u4e0d\u7b97\u591a\uff0c\u8fd9\u548c\u5927\u591a\u6570\u516c\u53f8\u6240\u91c7\u7528\u7684\u6280\u672f\u6709\u5173\u3002\u968f\u7740 java \u7684\u53d1\u5c55\uff0c\u6211\u8ba4\u4e3a\u51fd\u6570\u5f0f\u7f16\u7a0b\u3001\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff0c\u50cf WebFlux \u8fd9\u79cd\u5f02\u6b65\u7684\u3001\u975e\u963b\u585e\u7684\u3001\u4e8b\u4ef6\u9a71\u52a8\u7684\u670d\u52a1\u6846\u67b6\u4f1a\u9010\u6e10\u6d41\u884c\u8d77\u6765\u3002\u6240\u4ee5\u6211\u5c31\u5f00\u59cb\u7814\u7a76\u5e76\u5b66\u4e60\u5173\u4e8e WebFlux \u8fd9\u4e2a\u6846\u67b6\u7528\u6cd5\uff0c\u672c\u6587\u662f WebFlux \u7cfb\u5217\u6559\u7a0b\u7684\u7b2c\u4e00\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\uff01\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"webflux\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/mvc-webflux.jpg\" style=\"width: 573px;height: 339px\" \/>\n<\/p>\n<p>\n\t\u5173\u4e8e WebFlux \u662f\u4ec0\u4e48\uff0c\u6211\u5c31\u4e0d\u4ecb\u7ecd\u4e86\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u53bb\u770b Spring \u7684\u5b98\u7f51\uff0c\u6216\u8005\u5728\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7\u91cc\u641c\u76f8\u5173\u7684\u89c6\u9891\u3002\u53e6\u5916\u6211\u5728\u767e\u5ea6\u767e\u5bb6\u91cc\u4e5f\u53d1\u4e86\u4e00\u7bc7\u6587\u7ae0\u6765\u4ecb\u7ecd\u5b83\uff0c\u8fd9\u91cc\u5c31\u76f4\u63a5\u5f00\u59cb\u64b8\u4ee3\u7801\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u5148\u65b0\u5efa\u4e00\u4e2a&nbsp;webflux-helloworld \u5de5\u7a0b\u3002maven \u7684 pom.xml \u914d\u7f6e\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;properties&gt;\r\n    &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\r\n    &lt;project.reporting.outputEncoding&gt;UTF-8&lt;\/project.reporting.outputEncoding&gt;\r\n    &lt;java.version&gt;1.8&lt;\/java.version&gt;\r\n&lt;\/properties&gt;\r\n\r\n&lt;dependencies&gt;\r\n    &lt;dependency&gt;\r\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n        &lt;artifactId&gt;spring-boot-starter-webflux&lt;\/artifactId&gt;\r\n    &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;\r\n\r\n&lt;build&gt;\r\n    &lt;plugins&gt;\r\n        &lt;plugin&gt;\r\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n            &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\r\n        &lt;\/plugin&gt;\r\n    &lt;\/plugins&gt;\r\n&lt;\/build&gt;<\/pre>\n<p>\n\t\u8fd9\u91cc\u7684&nbsp;spring-webflow \u6211\u4f7f\u7528\u7684\u662f&nbsp;2.5.0.RELEASE \u7684\u7248\u672c\u3002\n<\/p>\n<p>\n\t\u7136\u540e\u662f&nbsp;WebFluxApplication.java \u7c7b\uff0c\u548c\u5176\u4ed6 SpringBoot \u542f\u52a8\u7c7b\u4e00\u6837\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@SpringBootApplication\r\npublic class WebFluxApplication {\r\n    public static void main(String[] args) {\r\n        SpringApplication.run(WebFluxApplication.class, args);\r\n    }\r\n}<\/pre>\n<p>\n\t\u518d\u63a5\u7740\uff0c\u6211\u4eec\u65b0\u5efa WebFlux \u7684 Router \u7c7b\uff0c\u7c7b\u4f3c SpringMVC \u4e2d\u7684 Controller \u3002\u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Configuration\r\npublic class HelloWebFlux {\r\n    \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    @Bean\r\n    public RouterFunction&lt;ServerResponse&gt; helloXttblog(XttblogHandler handler) {\r\n        return RouterFunctions\r\n                .route(RequestPredicates.GET(&quot;\/hello&quot;)\r\n                                .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)),\r\n                        handler::helloXttblog);\r\n    }\r\n}<\/pre>\n<p>\n\t\u63a5\u7740\u521b\u5efa Handler \u7c7b\u3002XttblogHandler.java \u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Component\r\npublic class XttblogHandler {\r\n    public Mono&lt;ServerResponse&gt; helloXttblog(ServerRequest request) {\r\n        return ServerResponse.ok().contentType(MediaType.TEXT_PLAIN)\r\n                .body(BodyInserters.fromObject(&quot;Hello, www.xttblog.com !&quot;));\r\n\r\n    }\r\n}<\/pre>\n<p>\n\t\u6700\u540e\u8fd0\u884c\u542f\u52a8 WebFluxApplication \u7c7b\u3002\u6211\u4eec\u5728\u6d4f\u89c8\u5668\u91cc\u8f93\u5165 http:\/\/localhost:8080\/hello\uff0c\u5373\u53ef\u770b\u5230\u5982\u4e0b\u7684\u5185\u5bb9\uff1a\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"webflux \u6848\u4f8b\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/webflux-hello.png\" style=\"width: 311px;height: 140px\" \/>\n<\/p>\n<p>\n\tWebFlux \u7684\u7aef\u53e3\u9ed8\u8ba4\u662f 8080\uff0c\u5728\u542f\u52a8\u7684\u65e5\u5fd7\u4e2d\u5c31\u53ef\u4ee5\u770b\u5230\u3002\n<\/p>\n<p>\n\t\u8fd9\u91cc\u5176\u5b9e\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u7684 fromObject \u65b9\u6cd5\u4f20\u9012\u7684\u662f\u4e00\u4e2a\u82f1\u6587\u7684\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u662f\u4e00\u4e2a\u5e26\u6709\u4e2d\u6587\u7684\u5b57\u7b26\u4e32\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u51fa\u73b0\u4e71\u7801\uff0c\u8fd9\u65f6\u6211\u4eec\u8be5\u600e\u4e48\u89e3\u51b3\u5462\uff1f\u505a\u6cd5\u5f88\u7b80\u5355\uff0c\u6211\u4eec\u4e0b\u7ae0\u518d\u89c1\uff01\n<\/p>\n<p>\n\t\u6e90\u7801\u5df2\u5206\u4eab\u5230\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7\uff0c\u626b\u63cf\u4e0b\u65b9\u4e8c\u7ef4\u7801\uff0c\u5173\u6ce8&ldquo;\u4e1a\u4f59\u8349&rdquo;\u56de\u590d\uff1a&ldquo;webflux&ldquo;\u5173\u952e\u5b57\u5373\u53ef\u514d\u8d39\u4e0b\u8f7d\uff01\n<\/p>\n<h2>\n\t\u53c2\u8003\u8d44\u6599<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t<a href=\"https:\/\/docs.spring.io\/spring\/docs\/5.0.0.BUILD-SNAPSHOT\/spring-framework-reference\/html\/web-reactive.html\" name=\"23. WebFlux framework\" target=\"_blank\" title=\"23. WebFlux framework\">23. WebFlux framework<\/a>\n\t<\/li>\n<\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3121\">Spring Boot WebFlux \u5165\u95e8\u4e4b Hello World<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>WebFlux \u5728\u56fd\u5916\u4f7f\u7528\u7684\u5df2\u7ecf\u975e\u5e38\u7684\u591a\u4e86\uff0c\u5728\u56fd\u5185\u7528\u7684\u4eba\u8fd8\u4e0d\u7b97\u591a\uff0c\u8fd9\u548c\u5927\u591a\u6570\u516c\u53f8\u6240\u91c7\u7528\u7684\u6280\u672f\u6709\u5173\u3002\u968f\u7740 jav [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[587,588,1701,621,1200],"class_list":["post-3121","post","type-post","status-publish","format-standard","hentry","category-java","tag-spring-boot","tag-spring-cloud","tag-webflux","tag-621","tag-1200"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3121"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3121\/revisions"}],"predecessor-version":[{"id":3134,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3121\/revisions\/3134"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}