如何使用新方式編寫Spring MVC接口
1. 前言
通常我們編寫 Spring MVC 接口的范式是這樣的:
@RestController@RequestMapping('/v1/userinfo')public class UserInfoController { @GetMapping('/foo') public String foo() { return 'felord.cn'; }}
這種我都寫吐了,今天換個(gè)口味,使用 Spring 5 新引入的函數(shù)式端點(diǎn)(Functional Endpoints)來耍耍。這種方式同樣支持 Spring Webflux。
請(qǐng)注意可使用該特性的 Spring 版本不低于 Spring 5.2
2. 依賴
為了演示,這里極簡(jiǎn)化只引入 Spring MVC 的 starter :
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
3. RouterFunction
在函數(shù)式端點(diǎn)的寫法中,傳統(tǒng)的請(qǐng)求映射(@RequestMapping)被路由函數(shù)(RouterFunction)所代替。上面的寫法等同于:
@Bean public RouterFunction<ServerResponse> fooFunction() { return RouterFunctions.route().GET('/v1/userinfo/foo', request -> ServerResponse.ok() .body('felord.cn')).build(); }
在該示例中,我使用了 RouterFunctions.route() 創(chuàng)建了一個(gè)RouterFunction,然后RouterFunction 提供了從請(qǐng)求到響應(yīng)的細(xì)節(jié)操作。
4. ServerRequest/ServerResponse
ServerRequest 是對(duì)服務(wù)器端的 HTTP 請(qǐng)求的抽象,你可以通過該抽象獲取請(qǐng)求的細(xì)節(jié)。對(duì)應(yīng)的,ServerResponse 是對(duì)服務(wù)器端響應(yīng)的抽象,你也可以通過該抽象構(gòu)建響應(yīng)的細(xì)節(jié)。這兩個(gè)概念由下面的 HandlerFunction 接口進(jìn)行 請(qǐng)求 → 響應(yīng) 處理。
5. HandlerFunction
HandlerFunction 是一個(gè)函數(shù)式接口,它提供了從請(qǐng)求( ServerRequest)到響應(yīng)(ServerResponse)的函數(shù)映射抽象。通常你的業(yè)務(wù)邏輯由該接口進(jìn)行實(shí)現(xiàn)。從 ServerRequest 中獲取請(qǐng)求的細(xì)節(jié),然后根據(jù)業(yè)務(wù)構(gòu)建一個(gè) ServerResponse 響應(yīng)。
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().body('felord.cn');
6. RequestPredicate
RequestPredicate 可以讓你根據(jù)請(qǐng)求的一些細(xì)節(jié),比如 請(qǐng)求方法、請(qǐng)求頭、請(qǐng)求參數(shù)等等進(jìn)行斷言以決定是否路由。
這里舉一個(gè)例子,假如我們希望請(qǐng)求接口/v1/userinfo/predicate時(shí)根據(jù)不同的參數(shù)處理不同的業(yè)務(wù),當(dāng)攜帶參數(shù) plan時(shí)才能進(jìn)行處理。我們可以這么寫:
@Bean public RouterFunction<ServerResponse> predicateFunction() { return RouterFunctions.route().GET('/v1/userinfo/predicate', request -> request.param('plan').isPresent(), request -> ServerResponse.ok().body('felord.cn')).build(); }
然后我們測(cè)試一下:
當(dāng)攜帶參數(shù) plan時(shí):
GET http://localhost:8080/v1/userinfo/predicate?plan=
HTTP/1.1 200Content-Type: text/plain;charset=UTF-8Content-Length: 9Date: Thu, 14 May 2020 07:57:35 GMTKeep-Alive: timeout=60Connection: keep-alive
felord.cn
不攜帶參數(shù)plan時(shí):
GET http://localhost:8080/v1/userinfo/predicateHTTP/1.1 404Vary: OriginVary: Access-Control-Request-MethodVary: Access-Control-Request-HeadersContent-Type: application/jsonTransfer-Encoding: chunkedDate: Thu, 14 May 2020 08:00:15 GMTKeep-Alive: timeout=60Connection: keep-alive{ 'timestamp': '2020-05-14T08:00:15.659+0000', 'status': 404, 'error': 'Not Found', 'message': 'No message available', 'path': '/v1/userinfo/predicate'}
7. 小結(jié)
函數(shù)式端點(diǎn)是 Spring 5 提供的一個(gè)新的接口范式風(fēng)格,對(duì)于 Spring MVC 來說 Spring 5.2 才進(jìn)行了支持。也是順應(yīng)函數(shù)式編程的一個(gè)未來趨勢(shì)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XHTML 1.0:標(biāo)記新的開端2. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?3. python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件4. Java 生成帶Logo和文字的二維碼5. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)6. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案7. xml中的空格之完全解說8. Android自定義控件實(shí)現(xiàn)方向盤效果9. Javaweb工程運(yùn)行報(bào)錯(cuò)HTTP Status 404解決辦法10. asp讀取xml文件和記數(shù)
