java - 当我使用 webflux 和 hatoas 时不显示绝对路径
问题描述
我正在阅读第 5 期的 spring in action并学习 spring-cloud、hateoas 和 webflux。我尝试编写如下的 rest 控制器
import static org.springframework.hateoas.server.reactive.WebFluxLinkBuilder.*;
@RestController
@RequestMapping(path = "/")
public class ServiceController {
private IngredientServiceClient ingredientClient;
@Autowired
public ServiceController(IngredientServiceClient ingredientClient) {
this.ingredientClient = ingredientClient;
}
@GetMapping("/ingredients/{id}")
public Mono<EntityModel<Ingredient>> getIngredientById(@PathVariable("id") String id) {
return ingredientClient.getIngredientById(id)
.flatMap(ingredient -> {
EntityModel<Ingredient> model = EntityModel.of(ingredient);
Mono<Link> link = linkTo(methodOn(ServiceController.class).getIngredientById(id)).withSelfRel().toMono();
return link.map(lk -> model.add(lk));
});
}
}
IngredientServiceClient.getIngredientById
public Mono<Ingredient> getIngredientById(String id) {
return wcBuilder.build()
.get().uri("http://ingredient-api/ingredients/{id}", id)
.retrieve().bodyToMono(Ingredient.class);
}
当我访问localhost:8082/ingredients/FLTO
我的 webapp 的一个节点时,它只向我显示这样的相对路径
{
"id": "FLTO",
"name": "Flour Tortilla",
"type": "WRAP",
"_links": {
"self": {
"href": "/ingredients/FLTO"
}
}
}
我已经尝试过,WebMvcLinkBuilder
但它仍然无法正常工作。我找到了一些关于我的问题的解释。但我不确定上下文/交换是否为空(以及为什么)。你可以帮帮我吗?
解决方案
尝试设置 spring.main.web-application-type=reactive
推荐阅读
- c# - 获取具有深层相关实体的对象
- php - 将 PHP 文件树数组转换为相对路径数组
- node.js - 限制 Parse Cloud Function 仅由经过身份验证的用户执行
- java - 使用 Java 增加/减少 AudioInputStream 的音频播放速度
- vue.js - 删除嵌套数组属性值时 UI 不更新,仅在添加时更新
- php - 用于匹配单引号字符串的正则表达式在 PHP 中失败
- python-3.x - OpenMDAO 版本 2.x 文件变量解决方法
- docker - 与 docker exec 混淆命令
- python-3.5 - 为什么 aiohttp import 显示以下错误?
- javascript - Node.js 无法识别 Ajax 请求