为了账号安全,请及时绑定邮箱和手机立即绑定

如何在属性文件中使用地图

如何在属性文件中使用地图

精慕HU 2024-01-25 15:19:31
如何在属性文件中映射它?我正在尝试遵循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 无法将此数据类型理解为属性。现在,您必须忽略解析错误。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

如果不起作用,请尝试删除[/**]结果中的方括号/**


查看完整回答
反对 回复 2024-01-25
?
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");

            }

        };

    }


查看完整回答
反对 回复 2024-01-25
  • 2 回答
  • 0 关注
  • 35 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信