proxy-api:
url-mapping:
{
"[/proxy/api/demo/list]": "http://127.0.0.1:9090/api/demo/list"
}
field-mapping:
{
"[/proxy/api/demo/list]": {
"success": "0000",
"code": "responseCode",
"msg": "message",
"data": "data"
}
}
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.util.Map;
@Data
@Configuration
@ConfigurationProperties(prefix = "proxy-api")
@PropertySource(value = {"classpath:proxy-mapping.yml"}, factory = YamlConfigFactory.class)
public class ProxyMappingConfig {
private Map<String, String> urlMapping;
private Map<String, Map<String, String>> fieldMapping;
}
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.util.Properties;
public class YamlConfigFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) {
String sourceName = resource.getResource().getFilename();
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
Properties propertiesFromYaml = factory.getObject();
assert sourceName != null;
assert propertiesFromYaml != null;
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
}
}