Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Jul 1, 2021
1 parent b1e2060 commit 711c919
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]

## 功能特性

- [x] [自定义注入OkHttpClient](#自定义注入OkHttpClient)

- [x] [注解式拦截器](#注解式拦截器)
- [x] [连接池管理](#连接池管理)
- [x] [日志打印](#日志打印)
Expand All @@ -39,6 +39,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
- [x] [全局拦截器](#全局拦截器)
- [x] [熔断降级](#熔断降级)
- [x] [微服务之间的HTTP调用](#微服务之间的HTTP调用)
- [x] [自定义注入OkHttpClient](#自定义注入OkHttpClient)
- [x] [调用适配器](#调用适配器)
- [x] [数据转换器](#数据转码器)

Expand All @@ -50,7 +51,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.11</version>
<version>2.2.12</version>
</dependency>
```

Expand All @@ -61,7 +62,7 @@ gitee项目地址:[https://gitee.com/lianjiatech/retrofit-spring-boot-starter]
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.11</version>
<version>2.2.12</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down Expand Up @@ -141,36 +142,12 @@ public class TestService {

## 配置项说明

`retrofit-spring-boot-starter`支持了多个可配置的属性,用来应对不同的业务场景。详细信息可参考[配置项示例](https://github.com/LianjiaTech/retrofit-spring-boot-starter/blob/master/src/test/resources/application.yml)
`retrofit-spring-boot-starter`支持了多个可配置的属性,用来应对不同的业务场景。**
详细信息可参考** [配置项示例](https://github.com/LianjiaTech/retrofit-spring-boot-starter/blob/master/src/test/resources/application.yml)

## 高级功能

### 自定义注入OkHttpClient

通常情况下,通过`@RetrofitClient`注解属性动态创建`OkHttpClient`对象能够满足大部分使用场景。但是在某些情况下,用户可能需要自定义`OkHttpClient`,这个时候,可以在接口上定义返回类型是`OkHttpClient.Builder`的静态方法来实现。代码示例如下:

```java
@RetrofitClient(baseUrl = "http://ke.com")
public interface HttpApi3 {

@OkHttpClientBuilder
static OkHttpClient.Builder okhttpClientBuilder() {
return new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS);

}

@GET
Result<Person> getPerson(@Url String url, @Query("id") Long id);
}
```

> 方法必须使用`@OkHttpClientBuilder`注解标记!


### 注解式拦截器

很多时候,我们希望某个接口下的某些http请求执行统一的拦截处理逻辑。为了支持这个功能,`retrofit-spring-boot-starter`提供了**注解式拦截器**,做到了**基于url路径的匹配拦截**。使用的步骤主要分为2步:
Expand Down Expand Up @@ -639,25 +616,51 @@ public ServiceInstanceChooser serviceInstanceChooser(LoadBalancerClient loadBala

#### 使用`@Retrofit`的`serviceId`和`path`属性,可以实现微服务之间的HTTP调用


```java
@RetrofitClient(serviceId = "${jy-helicarrier-api.serviceId}", path = "/m/count", errorDecoder = HelicarrierErrorDecoder.class)
@Retry
public interface ApiCountService {
}
```

### 自定义注入OkHttpClient

通常情况下,通过`@RetrofitClient`注解属性动态创建`OkHttpClient`对象能够满足大部分使用场景。但是在某些情况下,用户可能需要自定义`OkHttpClient`
,这个时候,可以在接口上定义返回类型是`OkHttpClient.Builder`的静态方法来实现。代码示例如下:

```java
@RetrofitClient(baseUrl = "http://ke.com")
public interface HttpApi3 {
@OkHttpClientBuilder
static OkHttpClient.Builder okhttpClientBuilder() {
return new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.SECONDS)
.writeTimeout(1, TimeUnit.SECONDS);
}
@GET
Result<Person> getPerson(@Url String url, @Query("id") Long id);
}
```

> 方法必须使用`@OkHttpClientBuilder`注解标记!

## 调用适配器和数据转码器

### 调用适配器

`Retrofit`可以通过调用适配器`CallAdapterFactory`将`Call<T>`对象适配成接口方法的返回值类型。`retrofit-spring-boot-starter`扩展2种`CallAdapterFactory`实现:
`Retrofit`可以通过调用适配器`CallAdapterFactory`将`Call<T>`对象适配成接口方法的返回值类型。`retrofit-spring-boot-starter`扩展2种`CallAdapterFactory`
实现:

1. `BodyCallAdapterFactory`
- 默认启用,可通过配置`retrofit.enable-body-call-adapter=false`关闭
- 同步执行http请求,将响应体内容适配成接口方法的返回值类型实例。
- 默认启用,可通过配置`retrofit.enable-body-call-adapter=false`关闭
- 同步执行http请求,将响应体内容适配成接口方法的返回值类型实例。
- 除了`Retrofit.Call<T>`、`Retrofit.Response<T>`、`java.util.concurrent.CompletableFuture<T>`之外,其它返回类型都可以使用该适配器。
2. `ResponseCallAdapterFactory`
- 默认启用,可通过配置`retrofit.enable-response-call-adapter=false`关闭
Expand Down
4 changes: 2 additions & 2 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.11</version>
<version>2.2.12</version>
</dependency>
```

Expand All @@ -53,7 +53,7 @@ This project depends on Retrofit-2.9.0, okhttp-3.14.9, and okio-1.17.5 versions.
<dependency>
<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.11</version>
<version>2.2.12</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.lianjiatech</groupId>
<artifactId>retrofit-spring-boot-starter</artifactId>
<version>2.2.11</version>
<version>2.2.12</version>

<name>retrofit-spring-boot-starter</name>
<description>retrofit-spring-boot-starter</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ protected HttpMethodPath parseHttpMethodPath(Method method) {
return new HttpMethodPath("HEAD", head.value());
}

if (method.isAnnotationPresent(PATCH.class)) {
PATCH patch = method.getAnnotation(PATCH.class);
return new HttpMethodPath("PATCH", patch.value());
}

return null;
}

Expand Down

0 comments on commit 711c919

Please sign in to comment.