异步API设计:电商应用的响应式API实现
1. 异常处理与HATEOAS引入
在完成异常处理的学习后,我们可以将注意力转向HATEOAS(Hypermedia as the Engine of Application State)。对于响应式API,HATEOAS的支持与之前使用Spring MVC时的操作有一定相似性。我们需要创建用于HATEOAS支持的装配器,同时借助HATEOAS装配器类实现模型与实体之间的相互转换。
Spring WebFlux提供了 ReactiveRepresentationModelAssembler 接口来构建超媒体链接。我们可以重写其 toModel() 方法,将链接添加到响应模型中。
首先,我们要做一些基础工作来填充链接。创建一个 HateoasSupport 接口,其中包含一个默认方法:
public interface HateoasSupport {
default UriComponentsBuilder getUriComponentBuilder(
@Nullable ServerWebExchange exchange) {
if (exchange == null) {
return UriComponentsBuilder.fromPath("/");
}
ServerHttpRequest request = exchange.getRequest();
PathContai
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



