Home > database >  Spring boot - multiple RestTemplate implementations based on Spring profile
Spring boot - multiple RestTemplate implementations based on Spring profile

Time:11-22

I'm trying to load different RestTemplates for different profiles. Here is how I implemented it:

RestTemplateProviderConfiguration.java

package com.my.configuration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

import static org.apache.commons.lang.StringUtils.equalsIgnoreCase;

@Configuration
public class RestTemplateProviderConfiguration {
    @Autowired
    private ApplicationContext appContext;

    @Value("${spring.profiles.active:}")
    private String activeProfile;

    @Value("${iam.rest.template:}")
    private String iamRestTemplateQualifier;

    @Bean
    public RestTemplate restTemplate() {
        return equalsIgnoreCase(activeProfile, "local") ? new RestTemplate() : (RestTemplate) appContext.getBean(iamRestTemplateQualifier);
    }
}

Service.java

...
@Autowired
private RestTemplateProviderConfiguration restTemplateProviderConfiguration;
...

public void testRestClient() {
    return restTemplateProviderConfiguration.restTemplate()
                    .exchange(...);
}

Can anyone suggest a better method where I can just use:

@Autowire
RestTemplate restTemplate;

and it will load the correct template based on the active spring profile?

CodePudding user response:

If you are using Spring 5.1.4 , you can specify @Profile annotation for your RestTemplate beans.

@Bean
@Profile("local")
public RestTemplate restTemplate() {
    return new RestTemplate();
}

@Bean
@Profile("!local")
public RestTemplate iamrestTemplate() {
    return new RestTemplate();
}

See javadoc for this annotation.

CodePudding user response:

You should have 2 different @Configuration, for each profile with equivalent profile as @Profile({"local"})

Each configuration will return different implementation of RestTemplate (and other relevant beans)

You can also define Profile per Bean, but it could be confusing

@Profile("profile")
@Bean
MyBean myBean(MyBeanProperties properties) {
  • Related