简体   繁体   English

如何在属性文件中使用 Map

[英]How to use a Map in a properties file

How to map this in a properties file?如何在属性文件中使用 map?

I'm trying to follow this documentation on Spring Cloud Gateway我正在尝试在 Spring Cloud Gateway 上关注此文档

However, we us application.properties.但是,我们使用application.properties。

spring:
  cloud:
    gateway:
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOrigins: "https://docs.spring.io"
            allowedMethods:
            - GET

I've tried different variations to no avail:我尝试了不同的变化无济于事:

spring.cloud.gateway.globalcors.cors-configurations./**.allowed-origin
spring.cloud.gateway.globalcors.cors-configurations.[/**].allowed-origin

I get an exception:我得到一个例外:

*************************** APPLICATION FAILED TO START ****************************** 应用程序无法启动


Description:描述:

Failed to bind properties under 'spring.cloud.gateway.globalcors.cors-configurations.allowed-origins' to org.springframework.web.cors.CorsConfiguration:无法将“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] [org.springframework.web.cors.CorsConfiguration]

Action:行动:

Update your application's configuration更新应用程序的配置

Please note that this code is using Spring Cloud Hoxton.M3.请注意,此代码使用 Spring Cloud Hoxton.M3。 I understand that one might assume that the known implementation as per the Spring Guide might be the answer but this is not the case as SC Gateway does not use HttpServlet anymore.我知道有人可能会假设 Spring 指南中的已知实现可能是答案,但情况并非如此,因为 SC Gateway 不再使用 HttpServlet。

Update: This works according to Marcos Barbero.更新:这适用于 Marcos Barbero。 Apparently, Eclipse can't understand this data type as a property.显然,Eclipse 无法将此数据类型理解为属性。 For now, you'll have to deal with ignoring the parsing error.现在,您必须处理忽略解析错误的问题。

spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods=*
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowCredentials=true

I didn't try it out, but I think you can use it like this:我没有尝试过,但我认为您可以像这样使用它:

spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedOrigins="https://docs.spring.io"
spring.cloud.gateway.globalcors.corsConfigurations.[/**].allowedMethods[0]=GET

If it doesn't work, try to remove the square brackets from [/**] resulting in /** .如果它不起作用,请尝试从[/**]中删除方括号,从而导致/**

you can't set these properties using propeties file.您不能使用属性文件设置这些属性。 Rather use Spring configuration to setup these properties something like below:而是使用 Spring 配置来设置这些属性,如下所示:

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/test-javaconfig").allowedOrigins("http://localhost:9000");
            }
        };
    }

Also, this post might be of some help.另外, 这篇文章可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM