換一種方式編寫 Spring MVC 接口
-
前言
通常我們編寫 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
-
依賴
為了示範,這裡極簡化隻引入 Spring MVC 的 starter :
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
-
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 提供了從請求到響應的細節操作。
-
ServerRequest/ServerResponse
ServerRequest 是對伺服器端的HTTP請求的抽象,你可以通過該抽象擷取請求的細節。對應的,ServerResponse 是對伺服器端響應的抽象,你也可以通過該抽象建構響應的細節。這兩個概念由下面的 HandlerFunction 接口進行 請求→ 響應 處理。
-
HandlerFunction
HandlerFunction 是一個函數式接口,它提供了從請求( ServerRequest)到響應(ServerResponse)的函數映射抽象。通常你的業務邏輯由該接口進行實作。從 ServerRequest 中擷取請求的細節,然後根據業務建構一個 ServerResponse 響應。
HandlerFunction handlerFunction = request -> ServerResponse.ok().body("felord.cn");
-
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時:
http://localhost:8080/v1/userinfo/predicateHTTP/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
{
"timestamp": "2020-05-14T08:00:15.659+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/v1/userinfo/predicate"
-
小結
函數式端點是 Spring 5 提供的一個新的接口範式風格,對于 Spring MVC 來說 Spring 5.2 才進行了支援。也是順應函數式程式設計的一個未來趨勢。由于篇幅原因這裡僅僅對其中的關鍵概念進行了講解。下一篇我們會對這種接口範式進行進一步的講解和實際使用。敬請關注:碼農小胖哥 。
原文位址
https://www.cnblogs.com/felordcn/p/12894127.html