pass different @Configuration bean to a rest based Client

僤鯓⒐⒋嵵緔 提交于 2021-01-04 20:42:50

问题


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:


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:


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;
    }
}


来源:https://stackoverflow.com/questions/63117202/pass-different-configuration-bean-to-a-rest-based-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!