換一種方式編寫(xiě) Spring MVC 接口

1. 前言

通常我們編寫(xiě) Spring MVC 接口的范式是這樣的:

@RestController
@RequestMapping("/v1/userinfo")
public class UserInfoController {
    
    @GetMapping("/foo")
    public String foo() {
        return "felord.cn";
    }
}

這種我都寫(xiě)吐了,今天換個(gè)口味,使用 Spring 5 新引入的函數(shù)式端點(diǎn)(Functional Endpoints)來(lái)耍耍。 這種方式同樣支持 Spring Webflux

請(qǐng)注意可使用該特性的 Spring 版本不低于 Spring 5.2

2. 依賴(lài)

為了演示,這里極簡(jiǎn)化只引入 Spring MVCstarter :

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

3. RouterFunction

在函數(shù)式端點(diǎn)的寫(xiě)法中,傳統(tǒng)的請(qǐng)求映射(@RequestMapping)被路由函數(shù)(RouterFunction)所代替。上面的寫(xiě)法等同于:

    @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)求的抽象,你可以通過(guò)該抽象獲取請(qǐng)求的細(xì)節(jié)。對(duì)應(yīng)的,ServerResponse 是對(duì)服務(wù)器端響應(yīng)的抽象,你也可以通過(guò)該抽象構(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)行處理。我們可以這么寫(xiě):

    @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 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 9
Date: Thu, 14 May 2020 07:57:35 GMT
Keep-Alive: timeout=60
Connection: keep-alive

felord.cn

不攜帶參數(shù)plan時(shí):

GET http://localhost:8080/v1/userinfo/predicate

HTTP/1.1 404 
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Transfer-Encoding: chunked
Date: Thu, 14 May 2020 08:00:15 GMT
Keep-Alive: timeout=60
Connection: 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 來(lái)說(shuō) Spring 5.2 才進(jìn)行了支持。也是順應(yīng)函數(shù)式編程的一個(gè)未來(lái)趨勢(shì)。由于篇幅原因這里僅僅對(duì)其中的關(guān)鍵概念進(jìn)行了講解。下一篇我們會(huì)對(duì)這種接口范式進(jìn)行進(jìn)一步的講解和實(shí)際使用。敬請(qǐng)關(guān)注:碼農(nóng)小胖哥

關(guān)注公眾號(hào):碼農(nóng)小胖哥,獲取更多資訊

個(gè)人博客:https://felord.cn

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容