如何在屬性文件中映射它?我正在嘗試遵循Spring Cloud Gateway 上的此文檔然而,我們使用application.properties。spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "https://docs.spring.io" allowedMethods: - GET我嘗試了不同的變體但無濟于事:spring.cloud.gateway.globalcors.cors-configurations./**.allowed-originspring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-origin我得到一個例外:************************** 應用程序無法啟動描述:無法將“spring.cloud.gateway.globalcors.cors-configurations.allowed-origins”下的屬性綁定到org.springframework.web.cors.CorsConfiguration:Reason: No converter found capable of converting from type [java.lang.String] to type[org.springframework.web.cors.CorsConfiguration]行動:更新您的應用程序的配置請注意,此代碼使用 Spring Cloud Hoxton.M3。我理解,人們可能會認為 Spring 指南中的已知實現可能就是答案,但事實并非如此,因為 SC Gateway 不再使用 HttpServlet。更新:根據馬科斯·巴貝羅的說法,這有效。顯然,Eclipse 無法將此數據類型理解為屬性?,F在,您必須忽略解析錯誤。spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins=*spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods=*spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowCredentials=true
2 回答

喵喔喔
TA貢獻1735條經驗 獲得超5個贊
我沒有嘗試過,但我認為你可以這樣使用它:
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins="https://docs.spring.io" spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods[0]=GET
如果不起作用,請嘗試刪除[/**]
結果中的方括號/**
。

catspeake
TA貢獻1111條經驗 獲得超0個贊
您無法使用屬性文件設置這些屬性。而是使用 Spring 配置來設置這些屬性,如下所示:
@Bean
? ? public WebMvcConfigurer corsConfigurer() {
? ? ? ? return new WebMvcConfigurerAdapter() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void addCorsMappings(CorsRegistry registry) {
? ? ? ? ? ? ? ? registry.addMapping("/test-javaconfig").allowedOrigins("http://localhost:9000");
? ? ? ? ? ? }
? ? ? ? };
? ? }
添加回答
舉報
0/150
提交
取消