pay-mall/pay-mall-app/src/main/java/edu/whut/config/Retrofit2Config.java

39 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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