英文:
pass different @Configuration bean to a rest based Client
问题
// app.yml
bus:
tyres: 8
seats: 40
color: red
url: www.businfo.com
car:
tyres: 4
seats: 6
color: blue
url: www.carinfo.com
// ConfigurationProperties class for BusConfig
@ConfigurationProperties("bus")
public class BusConfig {
public int tyres;
public int seats;
public String color;
public String url;
// setters and getters below.
}
// ConfigurationProperties class for CarConfig
@ConfigurationProperties("car")
public class CarConfig {
public int tyres;
public int seats;
public String color;
public String url;
// setters and getters below.
}
// Rest client to fetch information
public class RestClientHelper {
public RestTemplate giveMeRestTemplate(Type config) {
return restTemplate; // using the above type which might have url to the specific api to call.
}
}
// Calling code to get different rest templates based on config
public class SomeClient {
@Autowired
RestClientHelper rch;
@Autowired
BusConfig bc;
@Autowired
CarConfig cc;
public void publishDetails() {
rch.giveMeRestTemplate(bc); // so if I send cc, then it should prepare a rest template for cc
}
}
英文:
I have two different configuration which i would be loading from application yml. The properties are same but the values might differ.
How do i make this work giveMeRestTemplate(Type config)
// app.yml
bus:
tyres:8
seats:40
color:red
url: www.businfo.com
car:
tyres:4
seats:6
color:blue
url: www.carinfo.com
So i have different ConfigruationProperties class for this like below one other as CarConfig
@ConfigurationProperties("bus")
public class BusConfig{
public int tyres;
public int seats;
public string color ;
public string url;
//setters and getters below.
}
Then i have a rest client which i use to invoke some api to fetch information. So this api can return information of different types of vehicles you can say.
public class RestClientHelper{
public RestTemplate giveMeRestTemplate(Type config);
{
return restTemplate; //using the above type which might have url to the specific api to call.
}
}
The idea is that the calling code can get different rest templates based on what config was sent to it.
public SomeClient{
@Autowired
RestClientHelper rch;
@Autowired
BusConfig bc;
@Autowired
CarConfig cc;
public void publishDetails(){
rch.giveMeRestTemplate(bc); //so if i send cc then it should prepare rest template for cc
}
}
答案1
得分: 1
以下是您提供的内容的翻译部分:
无论是由@Archie(谢谢)[此处][1] 发布的内容都让我对编写代码有了更深的理解。
[1]: https://stackoverflow.com/a/63117456/3560140
public enum Type {
BUS, CAR
}
因此,将字符串存储为映射中的键,告诉我这个配置属于哪种特定的类型。
@ConfigurationProperties("rest-config")
public class RestConfig {
private Map<String, ConfigType> type = new HashMap<>();
public static class ConfigType {
private int tyres;
private int seats;
private String color;
private String url;
}
}
因此,助手可以接受一个实际的配置类型作为参数(这是我的调用代码可以根据它发送的类型来创建模板,而不是在此方法内部读取配置类型的方式)。
public class RestClientHelper {
public RestTemplate giveMeRestTemplate(RestConfig.ConfigType config) {
return restTemplate; //使用上述类型,该类型可能具有调用特定API的url。
}
}
客户端代码
public SomeClient {
@Autowired
RestClientHelper rch;
public void publishDetails() {
rch.giveMeRestTemplate(rch.type.get(Type.BUS.toString())); //我通过使用枚举来匹配字符串名称来发送实际类型
}
}
请注意,我只翻译了您提供的代码部分,如有其他问题或需要进一步帮助,请随时提问。
英文:
Whatever posted by @Archie(Thanks) here gave me insight to write it like this.
public enum Type {
BUS, CAR
}
So storing the string as key in map,which tell me to which specific type this config is.
@ConfigurationProperties("rest-config")
public class RestConfig {
private Map<String, ConfigType> type = new HashMap<>();
public static class ConfigType {
private int tyres;
private int seats;
private string color;
private string url;
}
}
So helper can take a type actual config type(this is where my caller code can send it a type based on which template can be created rather than reading inside this method about which type of config it is.)
public class RestClientHelper{
public RestTemplate giveMeRestTemplate(RestConfig.type config);
{
return restTemplate; //using the above type which might have url to the specific api to call.
}
}
Client Code
public SomeClient{
@Autowired
RestClientHelper rch;
public void publishDetails(){
rch.giveMeRestTemplate(rch.type.get(Type.BUS.toString())); //I am sending a actual type by using enum to match the string name
}
}
答案2
得分: 0
我建议稍微调整您的配置属性(为了简洁起见,省略了 getter 和 setter 方法):
public enum Type {
BUS, CAR
}
@ConfigurationProperties("rest-config")
public class RestConfig {
private Map<Type, ConfigType> type = new HashMap<>();
public static class ConfigType {
private int tyres;
private int seats;
private String color;
private String url;
}
}
通过这样,您可以拥有以下的配置文件:
rest-config:
type:
bus:
tyres: 8
seats: 40
color: red
url: www.businfo.com
car:
tyres: 4
seats: 6
color: blue
url: www.carinfo.com
最后是您的辅助类:
@Service
public class RestClientService {
@Autowired
private RestConfig config;
public RestTemplate giveMeRestTemplate(Type type) {
RestConfig.ConfigType cfg = config.getType().get(type);
// 根据 cfg 进行必要的操作
return restTemplate;
}
}
英文:
I'd suggest changing you configuration properties a bit (getters and setters omitted for brevity):
public enum Type {
BUS, CAR
}
@ConfigurationProperties("rest-config")
public class RestConfig {
private Map<Type, ConfigType> type = new HashMap<>();
public static class ConfigType {
private int tyres;
private int seats;
private string color;
private string url;
}
}
With that you can have the following configuration file:
rest-config:
type:
bus:
tyres: 8
seats: 40
color: red
url: www.businfo.com
car:
tyres: 4
seats: 6
color: blue
url: www.carinfo.com
And finally your helper:
@Service
public class RestClientService {
@Autowired
private RestConfig config;
public RestTemplate giveMeRestTemplate(Type type) {
RestConfig.ConfigType cfg = config.getType().get(type);
// do what's necessary with cfg
return restTemplate;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论