首页 > 解决方案 > 当我使用 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但它仍然无法正常工作。我找到了一些关于我的问题的解释。但我不确定上下文/交换是否为空(以及为什么)。你可以帮帮我吗?

标签: javaspring-webfluxspring-hateoas

解决方案


尝试设置 spring.main.web-application-type=reactive


推荐阅读