All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.hy2cone.retrofit.spring.boot.autoconfigure.RetrofitAutoConfiguration Maven / Gradle / Ivy

package com.github.hy2cone.retrofit.spring.boot.autoconfigure;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.support.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Configuration("r2ac")
@EnableConfigurationProperties(RetrofitProperties.class)
@ConditionalOnClass({Retrofit.class, JacksonConverterFactory.class})
public class RetrofitAutoConfiguration {
    @Resource
    private DefaultListableBeanFactory defaultListableBeanFactory;
    @Resource
    private RetrofitProperties retrofitProperties;

    @Resource(name = "r2OkHttpClient")
    private OkHttpClient okHttpClient;
    @Resource(name = "r2ObjectMapper")
    private ObjectMapper objectMapper;
    @Resource(name = "r2JacksonConverterFactory")
    private JacksonConverterFactory jacksonConverterFactory;

    @ConditionalOnMissingBean
    @Bean(name = "r2ObjectMapper")
    public ObjectMapper objectMapper() {
        return new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @ConditionalOnMissingBean
    @Bean(name = "r2JacksonConverterFactory")
    @ConditionalOnBean(name = "r2ObjectMapper")
    public JacksonConverterFactory jacksonConverterFactory() {
        return JacksonConverterFactory.create(objectMapper);
    }

    @ConditionalOnMissingBean
    @Bean(name = "r2OkHttpClient")
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder().build();
    }

    private  T createService(String baseUrl, Class serviceClass) {
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(jacksonConverterFactory);
        Retrofit retrofit = builder.client(okHttpClient).build();
        return retrofit.create(serviceClass);
    }

    @PostConstruct
    public void registerService() {
        List services = retrofitProperties.getServices();

        services.forEach(service -> {
            Class serviceClass = service.getRef();
            String baseUrl = service.getBaseUrl();
            String simpleName = serviceClass.getSimpleName();
            String beanName = simpleName.substring(0, 1).toLowerCase() + simpleName.substring(1);

            BeanDefinitionBuilder definitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(serviceClass);
            AbstractBeanDefinition beanDefinition = definitionBuilder.getBeanDefinition();
            beanDefinition.setInstanceSupplier(() -> createService(baseUrl, serviceClass));
            beanDefinition.setDescription(baseUrl);
            defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinition);
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy