NeverGiveUp161
NeverGiveUp161

Reputation: 850

pass different @Configuration bean to a rest based Client

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

Upvotes: 2

Views: 1204

Answers (2)

NeverGiveUp161
NeverGiveUp161

Reputation: 850

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

Upvotes: 1

Archie
Archie

Reputation: 6844

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

Upvotes: 1

Related Questions