首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在Spring WebFlux中以编程方式配置请求上下文属性

如何在Spring WebFlux中以编程方式配置请求上下文属性
EN

Stack Overflow用户
提问于 2018-08-14 12:15:46
回答 1查看 697关注 0票数 1

根据this的评论,Spring WebFlux支持spring.freemarker.request-context-attribute,实际上它工作正常。但是现在,我必须提供我的DelegatingWebFluxConfiguration扩展。根据official guidepost,我只按如下方式扩展了DelegatingWebFluxConfiguration类:

代码语言:javascript
代码运行次数:0
运行
复制
@Configuration
public class MyDelegatingWebFluxConfiguration extends DelegatingWebFluxConfiguration {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.freeMarker();
    }
}

然后,除了request-context-attribute之外,一切看起来都很好,因为我得到了一个例外:

代码语言:javascript
代码运行次数:0
运行
复制
freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> request  [in template "index.ftl" at line 8, column 20]

Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

FTL stack trace ("~" means nesting-related):
- Failed at: ${request.contextPath}  [in template "index.ftl" at line 8, column 18]

其中request的引用就是spring.freemarker.request-context-attribute的值。

那么,有没有人可以帮忙处理这个案子呢?

EN

回答 1

Stack Overflow用户

发布于 2018-08-18 03:55:01

根据Spring Framework参考,你只需要有一个实现WebFluxConfigurer的配置类(你不应该在它上面有@EnableWebFluxas it will disable Spring Boot auto-configuration)。

扩展DelegatingWebFluxConfiguration与在配置类上扩展@EnableWebFlux相同,它将告诉Spring Boot您希望完全控制WebFlux基础设施设置。

你不需要自己设置Freemarker,Spring Boot会帮你设置,你只需要在你的application.properties中进行配置即可。

使用WebFluxConfigurer应该可以解决这里的问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51833615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档