
1. 前言
通常我們撰寫 Spring MVC 介面的范式是這樣的:
@RestController
@RequestMapping("/v1/userinfo")
public class UserInfoController {
@GetMapping("/foo")
public String foo() {
return "felord.cn";
}
}
這種我都寫吐了,今天換個口味,使用 Spring 5 新引入的函式式端點(Functional Endpoints)來耍耍, 這種方式同樣支持 Spring Webflux,
請注意可使用該特性的 Spring 版本不低于 Spring 5.2
2. 依賴
為了演示,這里極簡化只引入 Spring MVC 的 starter :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3. RouterFunction
在函式式端點的寫法中,傳統的請求映射(@RequestMapping)被路由函式(RouterFunction)所代替,上面的寫法等同于:
@Bean
public RouterFunction<ServerResponse> fooFunction() {
return RouterFunctions.route()
.GET("/v1/userinfo/foo", request -> ServerResponse.ok()
.body("felord.cn"))
.build();
}
在該示例中,我使用了 RouterFunctions.route() 創建了一個RouterFunction,然后RouterFunction 提供了從請求到回應的細節操作,
4. ServerRequest/ServerResponse
ServerRequest 是對服務器端的HTTP請求的抽象,你可以通過該抽象獲取請求的細節,對應的,ServerResponse 是對服務器端回應的抽象,你也可以通過該抽象構建回應的細節,這兩個概念由下面的 HandlerFunction 介面進行 請求→ 回應 處理,
5. HandlerFunction
HandlerFunction 是一個函式式介面,它提供了從請求( ServerRequest)到回應(ServerResponse)的函式映射抽象,通常你的業務邏輯由該介面進行實作,從 ServerRequest 中獲取請求的細節,然后根據業務構建一個 ServerResponse 回應,
HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().body("felord.cn");
6. RequestPredicate
RequestPredicate 可以讓你根據請求的一些細節,比如 請求方法、請求頭、請求引數等等進行斷言以決定是否路由,

這里舉一個例子,假如我們希望請求介面/v1/userinfo/predicate時根據不同的引數處理不同的業務,當攜帶引數 plan時才能進行處理,我們可以這么寫:
@Bean
public RouterFunction<ServerResponse> predicateFunction() {
return RouterFunctions.route()
.GET("/v1/userinfo/predicate",
request -> request.param("plan").isPresent(),
request -> ServerResponse.ok().body("felord.cn"))
.build();
}
然后我們測驗一下:
當攜帶引數 plan時:
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
不攜帶引數plan時:
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. 小結
函式式端點是 Spring 5 提供的一個新的介面范式風格,對于 Spring MVC 來說 Spring 5.2 才進行了支持,也是順應函式式編程的一個未來趨勢,由于篇幅原因這里僅僅對其中的關鍵概念進行了講解,下一篇我們會對這種介面范式進行進一步的講解和實際使用,敬請關注:碼農小胖哥 ,
關注公眾號:Felordcn 獲取更多資訊
個人博客:https://felord.cn
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/194752.html
標籤:Java
上一篇:Java提取字串中的數字
