spring 无法让Java在Sping Boot 应用中看到我的自定义YAML属性

dly7yett  于 2024-01-05  发布在  Spring
关注(0)|答案(2)|浏览(173)

为什么不管用?

  1. gateway:
  2. servers:
  3. - local-server:
  4. url: https://localhost:${server.port}
  5. description: Api-Gateway-V2
  6. v1-prefix: /api/v1

x

  1. package by.afinny.apigateway.constant;
  2. import io.swagger.v3.oas.models.servers.Server;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Component;
  5. import java.util.List;
  6. @Component
  7. public final class GatewayMeta {
  8. @Value("${gateway.servers}")
  9. public List<Server> servers;
  10. @Value("${gateway.v1-prefix}")
  11. public String v1Prefix;
  12. public List<Server> servers() {
  13. return servers;
  14. }
  15. public String v1Prefix() {
  16. return v1Prefix;
  17. }
  18. }
  1. import io.swagger.v3.oas.models.annotations.OpenAPI31;
  2. import java.util.LinkedHashMap;
  3. import java.util.Map;
  4. import java.util.Objects;
  5. public class Server {
  6. private String url = null;
  7. private String description = null;
  8. private ServerVariables variables = null;
  9. private Map<String, Object> extensions = null;
  10. public Server() {
  11. }
  12. // ...
  1. Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'gateway.servers' in value "${gateway.servers}"
  2. at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-6.0.12.jar:6.0.12]
  3. at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-6.0.12.jar:6.0.12]
  4. at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-6.0.12.jar:6.0.12]

的数据
我尝试按照this advice添加一些“configuration processor”作为常规依赖项,但它不起作用(我是否应该将其 Package 在一些“processor”标记中?)

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-configuration-processor</artifactId>
  4. <optional>true</optional>
  5. </dependency>


在构建后,我的target中没有任何META-INF(should I?)
Sping Boot 3,Java 17

UPD

我尝试了这个

  1. package by.afinny.apigateway.constant;
  2. import io.swagger.v3.oas.models.servers.Server;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5. import java.util.List;
  6. @Component
  7. @Setter
  8. @ConfigurationProperties(value = "gateway")
  9. public final class GatewayMeta {
  10. public List<Server> servers;
  11. public String v1Prefix;
  12. public List<Server> servers() {
  13. return servers;
  14. }
  15. public String v1Prefix() {
  16. return v1Prefix;
  17. }
  18. }
  1. # notice the camel case
  2. gateway:
  3. servers:
  4. - local-server:
  5. url: https://localhost:${server.port}
  6. description: Api-Gateway-V2
  7. v1Prefix: /api/v1

的字符串
我得到一个异常。它相对详细,但我仍然不明白我需要做什么。如何更新?

  1. ERROR 19960 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
  2. ***************************
  3. APPLICATION FAILED TO START
  4. ***************************
  5. Description:
  6. Binding to target [Bindable@2a484710 type = java.util.List<io.swagger.v3.oas.models.servers.Server>, value = 'none', annotations = array<Annotation>[[empty]], bindMethod = [null]] failed:
  7. Property: gateway.servers[0].local-server.description
  8. Value: "Api-Gateway-V2"
  9. Origin: class path resource [application-prod.yml] - 53:22
  10. Reason: The elements [gateway.servers[0].local-server.description,gateway.servers[0].local-server.url] were left unbound.
  11. Property: gateway.servers[0].local-server.url
  12. Value: "https://localhost:${server.port}"
  13. Origin: class path resource [application-prod.yml] - 52:14
  14. Reason: The elements [gateway.servers[0].local-server.description,gateway.servers[0].local-server.url] were left unbound.
  15. Action:
  16. Update your application's configuration


我还将@EnableConfigurationProperties包含在@SpringBootApplication类的上面(我偶然发现了它,并决定将其放在以防万一)
我发现它与Server序列化有关:如果我注解掉与服务器相关的所有内容,前缀确实会被解析。
此外,我不希望我的yaml被迫遵循 Camel 的情况,我还没有找到@ConfigurationProperties@JsonProperty的等价物(比如@ConfigurationProperty之类的)。

bz4sfanl

bz4sfanl1#

对于托管的复杂列表,建议使用ConfigurationProperties。我认为这是你的yml结构错误。
我在这里复制了案例https://github.com/sbernardo/spring-issues-examples/tree/main/sof-questions-77646394
旧yml:

  1. gateway:
  2. servers:
  3. - local-server:
  4. url: https://localhost:${server.port}
  5. description: Api-Gateway-V2
  6. v1Prefix: /api/v1

字符串
新yml:

  1. gateway:
  2. servers:
  3. - local-server:
  4. url: https://localhost:${server.port}
  5. description: Api-Gateway-V2
  6. - local-server:
  7. url: https://localhost2:${server.port}
  8. description: Api-Gateway-V3
  9. v1Prefix: /api/v1


正如你所看到的,有一个级别会抛出异常。因为在yaml中,你用- simbol定义和数组,只有local-server键,你在每个对象上添加另一个级别,字段为urldescription字段。
我认为关键local-server是无用的,你可以删除它.
希望这会有所帮助:)

展开查看全部
lsmd5eda

lsmd5eda2#

在Spring中,当使用@Value annotation和${gateway.servers}这样的占位符时,Spring希望这些值在应用程序的属性源中可用,通常在application.propertiesapplication.yml中定义。在您的情况下,似乎没有解析${gateway.servers}属性。
如果你有另一个文件,你可以把它包含在你的Pom.xml中。

  1. <build>
  2. <resources>
  3. <resource>
  4. <directory>${PATH-DIR}</directory>
  5. <includes>
  6. <include>${FILE-NAME}</include>
  7. </includes>
  8. </resource>
  9. </resources>
  10. </build>

字符串

相关问题