传递不同的@Configuration bean 给基于 REST 的客户端

huangapple go评论79阅读模式
英文:

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(&quot;rest-config&quot;)
    public class RestConfig {
        private Map&lt;String, ConfigType&gt; type = new HashMap&lt;&gt;();
        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(&quot;rest-config&quot;)
public class RestConfig {
    private Map&lt;Type, ConfigType&gt; type = new HashMap&lt;&gt;();

    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&#39;s necessary with cfg
        return restTemplate;
    }
}

huangapple
  • 本文由 发表于 2020年7月27日 22:13:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63117202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定