gpt4 book ai didi

java - Webflux Reactive 对象在渲染 Thymeleaf View 之前未解析

转载 作者:行者123 更新时间:2023-12-02 08:39:26 25 4
gpt4 key购买 nike

当我尝试在 Thymeleaf 中渲染 View 时,出现错误 由以下原因引起:org.springframework.expression.spel.SpelEvaluationException: EL1008E: 在“reactor”类型的对象上找不到属性或字段“currentTemperature”。 core.publisher.MonoMapFuseable' - 可能不公开或无效?

Spring WebFlux 文档指出“具有响应式类型包装器的模型属性将解析为其实际值”,但是将 Mono<> 作为模型传递给 View 会出现上述错误。

  @RequestMapping(path = "/")
@GetMapping
public String home(Model model) {
Mono<ThermostatState> thermostatState = thermostatClient.fetchThermostatState();
model.addAttribute("thermostatState", thermostatState);
return "home";
}

阻止 Mono<> 并展开内部值会使模板呈现不变,但有点消除了使用响应式(Reactive)库的意义。

  @RequestMapping(path = "/")
@GetMapping
public String home(Model model) {
Mono<ThermostatState> thermostatState = thermostatClient.fetchThermostatState();
ThermostatState unwrappedState = thermostatState.block();
model.addAttribute("thermostatState", unwrappedState);
return "home";
}

该项目完全依赖于 spring starter 依赖项,并且没有显式的配置类。

最佳答案

嗨,我也是 Spring 和响应式编程的新手;但我认为,可以这样处理。

 @RequestMapping(path = "/")
@GetMapping
public Mono<String> home(Model model) {
return thermostatClient.fetchThermostatState()
.map(thermostatState -> {
model.addAttribute("thermostatState", thermostatState);
return "home";
});
}

关于java - Webflux Reactive 对象在渲染 Thymeleaf View 之前未解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61470200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com