为什么不管用?
gateway:
servers:
- local-server:
url: https://localhost:${server.port}
description: Api-Gateway-V2
v1-prefix: /api/v1
x
package by.afinny.apigateway.constant;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public final class GatewayMeta {
@Value("${gateway.servers}")
public List<Server> servers;
@Value("${gateway.v1-prefix}")
public String v1Prefix;
public List<Server> servers() {
return servers;
}
public String v1Prefix() {
return v1Prefix;
}
}
import io.swagger.v3.oas.models.annotations.OpenAPI31;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
public class Server {
private String url = null;
private String description = null;
private ServerVariables variables = null;
private Map<String, Object> extensions = null;
public Server() {
}
// ...
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'gateway.servers' in value "${gateway.servers}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-6.0.12.jar:6.0.12]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-6.0.12.jar:6.0.12]
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”标记中?)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
型
在构建后,我的target
中没有任何META-INF(should I?)
Sping Boot 3,Java 17
UPD
我尝试了这个
package by.afinny.apigateway.constant;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@Setter
@ConfigurationProperties(value = "gateway")
public final class GatewayMeta {
public List<Server> servers;
public String v1Prefix;
public List<Server> servers() {
return servers;
}
public String v1Prefix() {
return v1Prefix;
}
}
# notice the camel case
gateway:
servers:
- local-server:
url: https://localhost:${server.port}
description: Api-Gateway-V2
v1Prefix: /api/v1
的字符串
我得到一个异常。它相对详细,但我仍然不明白我需要做什么。如何更新?
ERROR 19960 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
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:
Property: gateway.servers[0].local-server.description
Value: "Api-Gateway-V2"
Origin: class path resource [application-prod.yml] - 53:22
Reason: The elements [gateway.servers[0].local-server.description,gateway.servers[0].local-server.url] were left unbound.
Property: gateway.servers[0].local-server.url
Value: "https://localhost:${server.port}"
Origin: class path resource [application-prod.yml] - 52:14
Reason: The elements [gateway.servers[0].local-server.description,gateway.servers[0].local-server.url] were left unbound.
Action:
Update your application's configuration
型
我还将@EnableConfigurationProperties
包含在@SpringBootApplication
类的上面(我偶然发现了它,并决定将其放在以防万一)
我发现它与Server
序列化有关:如果我注解掉与服务器相关的所有内容,前缀确实会被解析。
此外,我不希望我的yaml被迫遵循 Camel 的情况,我还没有找到@ConfigurationProperties
的@JsonProperty
的等价物(比如@ConfigurationProperty
之类的)。
2条答案
按热度按时间bz4sfanl1#
对于托管的复杂列表,建议使用
ConfigurationProperties
。我认为这是你的yml结构错误。我在这里复制了案例https://github.com/sbernardo/spring-issues-examples/tree/main/sof-questions-77646394
旧yml:
字符串
新yml:
型
正如你所看到的,有一个级别会抛出异常。因为在yaml中,你用
-
simbol定义和数组,只有local-server
键,你在每个对象上添加另一个级别,字段为url
和description
字段。我认为关键
local-server
是无用的,你可以删除它.希望这会有所帮助:)
lsmd5eda2#
在Spring中,当使用
@Value
annotation和${gateway.servers}
这样的占位符时,Spring希望这些值在应用程序的属性源中可用,通常在application.properties
或application.yml
中定义。在您的情况下,似乎没有解析${gateway.servers}
属性。如果你有另一个文件,你可以把它包含在你的
Pom.xml
中。字符串