- 前面初体验之后,我们尝试一下进行route Path的迁移,我们实战只写日常用到的配置实现方式,不具体分析原理,具体原理后续分析。在传统springMvc项目中我们使用Controller来处理serverlet的请求,在spring 5.0中的reactive方式中,对这些做了基本的兼容,如果把传统编码方式的项目迁移到react,只需要做一些特定的调整,在react mvc中,推荐使用handler替代Controller来做路由数据处理,下面将两种方式对比着做对照梳理。
- 在React模式中,Controller数据返回和入参处理都是通过Mono<T>、Flux<T>来处理,两个是实现了Publisher<T>接口入参中可以注入org.springframeworl.http.server.reactive.ServerHttpRequest
- Handler比较简单入参和出参分别为ServerRequest 和Mono<ServerResponse>
- 返回简单字符的GET请求
@RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
public Mono<String> getUserBase() {
return Mono.create(stringMonoSink -> stringMonoSink.success("getUserBase:" + Math.random()));
}
Handler模式
public Mono<ServerResponse> getAddress(ServerRequest request) {
return ServerResponse.ok().contentType(APPLICATION_JSON_UTF8).syncBody("ooooooooooooooooo2");
}
2. Rest PathVariable 风格
Controller 模式
@GetMapping(value = {"/name/{name}"})
public Mono<String> getUserName(@PathVariable String name) {
return Mono.create(stringMonoSink -> stringMonoSink.success("getUserName:" + name));
}
Handler 模式
public Mono<ServerResponse> getName