[英]How to define null object value for key-value pair in yaml in Spring Boot?
[英]Spring Boot Loads wrong key-value pair for map from YAML configureation when '-' hyphen OR '_' underscore is used
我正在使用自定義的“YamlPropertySourceFactory”在 Spring Boot 中加載 yaml 配置。 當配置作為 Map 加載時,它會為下面的場景加載錯誤的鍵值對值。
SpringBoot 'PropertySourceFactory' 假定鍵值 'CORE_3_1' 和 'CORE_31' OR 'CORE-3-1' 和 'CORE-31' 相同。
下面是復制此問題的示例代碼。
Yaml 配置- response-mapping.yaml
mappings:
response-code:
mappings:
CORE_3_1: "30"
CORE_31: "31"
CORE_32: "32"
或者
Yaml 配置- response-mapping.yaml
mappings:
response-code:
mappings:
CORE-3-1: "30"
CORE-31: "31"
CORE-32: "32"
自定義 YamlPropertySourceFactory
import java.util.Properties;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
public final class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertiesPropertySource createPropertySource(@SuppressWarnings("unused") final String name, final EncodedResource encodedResource) {
final YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(encodedResource.getResource());
final Properties properties = factory.getObject();
return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
}
}
響應配置類
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
/**
* It is used to load response codes from YAML configuration.
*/
@Configuration
@ConfigurationProperties(prefix = "mappings.response-code")
@PropertySource(value = "classpath:response-mapping.yaml", factory = YamlPropertySourceFactory.class)
public class ResponseCode {
private Map<String, String> mappings;
public Map<String, String> getMappings() {
return this.mappings;
}
public String getMapping( final String errorCode) {
return this.mappings.get(errorCode);
}
public void setMappings(final Map<String, String> value) {
this.mappings = value;
}
@Override
public String toString() {
return "ResponseCode{" + "mappings=" + this.mappings + '}';
}
}
簡單的控制器來測試它。 - http://localhost:8080/code
package de.fiserv.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private ResponseCode responseCode;
@GetMapping(value = "/code")
public ResponseEntity<String> code() {
return new ResponseEntity<>(this.responseCode.getMappings().toString(), HttpStatus.OK);
}
}
我找到了解決方法,將 '-' OR '_' 替換為 '.' 在 key 中它可以工作,但它會破壞錯誤代碼映射的整個設計。
在以下位置找到解決方案: https : //docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding.environment-variables
更新的 Yaml 配置- response-mapping.yaml
mappings:
response-code:
mappings:
"[CORE_3_1]": "30"
CORE_31: "31"
為什么會這樣?
Spring Boot 使用一些寬松的規則將 Environment 屬性綁定到 @ConfigurationProperties bean,因此 Environment 屬性名稱和 bean 屬性名稱之間不需要完全匹配。 這很有用的常見示例包括以破折號分隔的環境屬性(例如,上下文路徑綁定到上下文路徑)和大寫的環境屬性(例如,端口綁定到端口)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.