1.加入依赖
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
2.以企业微信api为例
创建 WorkWeiXinApi 接口
加入注解 RetrofitClient 配置 baseUrl
@RetrofitClient(baseUrl = "https://qyapi.weixin.qq.com/cgi-bin/service/")
public interface WorkWeiXinApi {
@POST("get_register_code")
Call<RegisterCodeResponse> getRegisterCode(@Query("provider_access_token") String providerAccessToken, @Body RegisterCodeRequest request);
@POST("get_provider_token")
Call<ProviderTokenResponse> getProviderToken(@Body ProviderTokenRequest request);
@GET("get_pre_auth_code")
Call<PreAuthCodeResponse> getPreAuthCode (@Query("suite_access_token") String suiteAccessToken);
@GET("get_suite_token")
Call<SuiteTokenResponse> getSuiteToken (@Body SuiteTokenRequest request);
}
启动类加上扫描包配置
@SpringBootApplication
@EnableConfigurationProperties(Config.class)
@RetrofitScan("cn.regent.work_weixin.api")
public class WorkWeixinApplication {
public static void main(String[] args) {
SpringApplication.run(WorkWeixinApplication.class, args);
}
}
3.调用
@Resource
private Config config;
@Resource
private WorkWeiXinApi workWeiXinApi;
/**
* 注册url
* @return
* @throws IOException
*/
@GetMapping("/register")
public RegisterUrlResponse getRegisterUrl() throws IOException {
RegisterUrlResponse registerUrlResponse = new RegisterUrlResponse();
String corpid = config.getCorpid();
String providerSecret = config.getProvider_secret();
//参数
ProviderTokenRequest providerTokenRequest = new ProviderTokenRequest();
providerTokenRequest.setCorpid(corpid);
providerTokenRequest.setProvider_secret(providerSecret);
//真正调用的地方
Response<ProviderTokenResponse> providerTokenResponse = workWeiXinApi.getProviderToken(providerTokenRequest).execute();
assert providerTokenResponse.body() != null;
String provider_access_token = providerTokenResponse.body().getProvider_access_token();
registerUrlResponse.setRegisterUrl(provider_access_token);
return registerUrlResponse;
}