39 lines
1.3 KiB
Java
39 lines
1.3 KiB
Java
package edu.whut.config;
|
||
|
||
import edu.whut.infrastructure.gateway.IWeixinApiService;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.springframework.context.annotation.Bean;
|
||
import org.springframework.context.annotation.Configuration;
|
||
import retrofit2.Retrofit;
|
||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||
|
||
@Slf4j
|
||
@Configuration
|
||
public class Retrofit2Config {
|
||
|
||
// 微信开放平台的基础 URL,后续所有接口都会在这个前缀下拼接路径
|
||
private static final String BASE_URL = "https://api.weixin.qq.com/";
|
||
|
||
/**
|
||
* 创建一个 Retrofit 对象,并注册为 Spring Bean
|
||
* - baseUrl:设置所有请求的公共前缀
|
||
* - addConverterFactory:添加 Jackson 转换器,用于 JSON <-> Java 对象的自动映射
|
||
*/
|
||
@Bean
|
||
public Retrofit retrofit() {
|
||
return new Retrofit.Builder()
|
||
.baseUrl(BASE_URL)
|
||
.addConverterFactory(JacksonConverterFactory.create()).build();
|
||
}
|
||
|
||
/**
|
||
* 通过 Retrofit 动态生成 IWeixinApiService 接口的实现,
|
||
* 并注册为 Spring 容器中的 Bean,方便业务层直接注入使用
|
||
*/
|
||
@Bean
|
||
public IWeixinApiService weixinApiService(Retrofit retrofit) {
|
||
return retrofit.create(IWeixinApiService.class);
|
||
}
|
||
|
||
}
|