簡體   English   中英

如何在Spring WebFlux中以編程方式配置請求上下文屬性

[英]How to configure request-context-attribute programmatically in Spring WebFlux

根據的評論 ,春WebFlux支持spring.freemarker.request-context-attribute ,實際上它工作正常。 但是現在,我必須提供DelegatingWebFluxConfiguration擴展。 根據官方指南帖子 ,我只是將DelegatingWebFluxConfiguration類擴展如下:

@Configuration
public class MyDelegatingWebFluxConfiguration extends DelegatingWebFluxConfiguration {
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.freeMarker();
    }
}

然后,除了request-context-attribute以外,其他一切似乎都很好,因為我有以下例外:

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的值。

因此,有人可以幫助處理此案嗎?

根據Spring Framework參考,您只需要具有一個實現WebFluxConfigurer的配置類(您不應在其上使用@EnableWebFlux因為它將禁用Spring Boot自動配置 )。

擴展DelegatingWebFluxConfiguration與配置類上的@EnableWebFlux相同,它將告訴Spring Boot您希望完全控制WebFlux基礎結構設置。

您不需要自己設置Freemarker,Spring Boot會為您完成,您只需在application.properties配置即可。

使用WebFluxConfigurer應該可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM