[英]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.