Call Rest Api by Retrofit2
功能: 通过 Retrofit2 调用远程 Api。
这里以调用微信小游戏的获取 access_token 功能为例。
pom.xml
添加 Retrofit 相关依赖。
xml
<!-- Retrofit -->
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-simplexml</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-jackson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>3.12.0</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WxApi.java
定义 Api 接口。
java
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface WxApi {
/**
* 小游戏 - 通过 code 获取 access_token
*
* @param appid 小程序唯一凭证,即 AppID,可在「微信公众平台 - 设置 - 开发设置」页中获得。(需要已经成为开发者,且帐号没有异常状态)
* @param secret 小程序唯一凭证密钥,即 AppSecret,获取方式同 appid
* @param code 登录时获取的 code
* @param grantType 固定传入 client_credential
* @return
*/
@GET("sns/jscode2session")
Call<WxSessionResponse> jsCode2Session(
@Query("appid") String appid,
@Query("secret") String secret,
@Query("js_code") String code,
@Query("grant_type") String grantType);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
WechatRetrofitConfigure.java
创建 wechatApiRetrofit Bean 。
java
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
@Component
public class WechatRetrofitConfigure {
@Bean("wechatApiRetrofit")
@ConditionalOnMissingBean(name = "wechatApiRetrofit")
public Retrofit createWechatApiRetrofit() {
return new Retrofit.Builder()
.baseUrl("https://api.weixin.qq.com/")
.addConverterFactory(JacksonConverterFactory.create())
.client(new OkHttpClient.Builder()
.addInterceptor((new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)))
.build()
)
.build();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
WxServiceImpl.java
调用时先通过 wechatApiRetrofit 创建 WxApi 的 Call 实例,之后通过调用其 execute 方法执行接口调用。
java
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import java.io.IOException;
/**
* 微信服务
*/
@RequiredArgsConstructor
@Service
@Slf4j
public class WxServiceImpl implements WxService {
private final Retrofit wechatApiRetrofit;
@Override
public WxSessionResponse jsCode2Session(String appid, String secret, String code, String clientCredential) {
Call<WxSessionResponse> call = wechatApiRetrofit.create(WxApi.class)
.jsCode2Session(appid, secret, code, clientCredential);
Response<WxSessionResponse> retrofitResponse = null;
try {
retrofitResponse = call.execute();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
assert retrofitResponse != null;
if (!retrofitResponse.isSuccessful()) {
throw new RuntimeException("获取微信用户信息, 网络异常");
}
return retrofitResponse.body();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39