首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >换一种方式编写 Spring MVC 接口

换一种方式编写 Spring MVC 接口

作者头像
码农小胖哥
发布2020-05-18 14:29:01
4640
发布2020-05-18 14:29:01
举报

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 MVCstarter :

 <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 才进行了支持。也是顺应函数式编程的一个未来趋势。由于篇幅原因这里仅仅对其中的关键概念进行了讲解。下一篇我们会对这种接口范式进行进一步的讲解和实际使用。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码农小胖哥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 前言
  • 2. 依赖
  • 3. RouterFunction
  • 4. ServerRequest/ServerResponse
  • 5. HandlerFunction
  • 6. RequestPredicate
  • 7. 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档