Skip to content

Commit

Permalink
fix the bug that the user can,t directly added the encoder and decode…
Browse files Browse the repository at this point in the history
…r through RestClientBuilder(#132)
  • Loading branch information
LCDZhao-Z authored Mar 8, 2022
2 parents 379fe5f + 8cd3fed commit 9389a1c
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ We used the following software for the testing:
-Xms1024m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:+UseConcMarkSweepGC -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70
```
### Ping Result
![ping_result](./docs/img/ping_result.png)
![ping_result](./site/content/en/img/ping_result.png)

### Client Options

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
*/
public class RestClientBuilder implements Reusable<RestClientBuilder>, RestClientOptions {

public static final String CLIENT = "RestClient";
private static final String CLIENT = "RestClient";

private static final Logger logger = LoggerUtils.logger();
private final HttpClientBuilder httpClientBuilder;
Expand Down Expand Up @@ -439,7 +439,6 @@ private void loadDecodeAdvicesFromSpi() {
decodeAdvicesFromSpi, decodeAdviceFactory);
}
});
OrderedComparator.sort(decodeAdvices);
}

private void sortDecodeAdvices() {
Expand Down Expand Up @@ -528,6 +527,8 @@ public RestClientBuilder copy() {
restClientBuilder.addInterceptors(interceptors);
restClientBuilder.addEncodeAdvices(encodeAdvices);
restClientBuilder.addDecodeAdvices(decodeAdvices);
restClientBuilder.addDecoders(decoders);
restClientBuilder.addEncoders(encoders);
return restClientBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ void testBasic() {
builder.addInterceptor(createInterceptor(2));
builder.addDecodeAdvice(createDecodeAdvice(2));
builder.addEncodeAdvice(createEncodeAdvice(2));
builder.addEncoder(createByteEncoder(-10000));
builder.addDecoder(createByteDecoder(-10000));

isEqual(builder, builder.copy());
isEqual(builder, builder.build().clientOptions());
Expand Down Expand Up @@ -165,9 +167,16 @@ void isEqual(RestClientOptions origin, RestClientOptions other) {
then(origin.version()).isEqualTo(other.version());
then(origin.isUseDecompress()).isEqualTo(other.isUseDecompress());
then(origin.isUseExpectContinue()).isEqualTo(other.isUseExpectContinue());

then(origin.unmodifiableDecodeAdvices().size()).isEqualTo(other.unmodifiableDecodeAdvices().size());
then(origin.unmodifiableEncodeAdvices().size()).isEqualTo(other.unmodifiableEncodeAdvices().size());
then(origin.unmodifiableInterceptors().size()).isEqualTo(other.unmodifiableInterceptors().size());

then(origin.unmodifiableDecodeAdvices().get(0)).isEqualTo(other.unmodifiableDecodeAdvices().get(0));
then(origin.unmodifiableEncodeAdvices().get(0)).isEqualTo(other.unmodifiableEncodeAdvices().get(0));
then(origin.unmodifiableInterceptors().get(0)).isEqualTo(other.unmodifiableInterceptors().get(0));
then(origin.unmodifiableEncoders().get(0)).isEqualTo(other.unmodifiableEncoders().get(0));
then(origin.unmodifiableDecoders().get(0)).isEqualTo(other.unmodifiableDecoders().get(0));
}

@Test
Expand All @@ -182,6 +191,9 @@ void testAddEncoder() {

List<Encoder> orderedEncoders = builder.unmodifiableEncoders();
then(orderedEncoders.size()).isEqualTo(3);
//unmodifiableEncoders().size() = encoders added(3) + encoders from spi(6)
then(builder.build().clientOptions().unmodifiableEncoders().size()).isEqualTo(9);
then(builder.build().clientOptions().unmodifiableEncoders().get(0)).isEqualTo(encoder2);
}

@Test
Expand All @@ -196,6 +208,9 @@ void testAddDecoder() {

List<Decoder> orderedDecoders = builder.unmodifiableDecoders();
then(orderedDecoders.size()).isEqualTo(3);
//unmodifiableDecoders().size() = decoders added(3) + decoders from spi(3)
then(builder.build().clientOptions().unmodifiableDecoders().size()).isEqualTo(6);
then(builder.build().clientOptions().unmodifiableDecoders().get(0)).isEqualTo(decoder2);
}

@Test
Expand All @@ -210,6 +225,8 @@ void testAddDecodeAdvice() {

List<DecodeAdvice> orderedDecodeAdvices = builder.unmodifiableDecodeAdvices();
then(orderedDecodeAdvices.size()).isEqualTo(3);
then(builder.build().clientOptions().unmodifiableDecodeAdvices().size()).isEqualTo(3);
then(builder.build().clientOptions().unmodifiableDecodeAdvices().get(0)).isEqualTo(decodeAdvice2);
}

@Test
Expand All @@ -224,6 +241,8 @@ void testAddEncodeAdvice() {

List<EncodeAdvice> orderedEncodeAdvices = builder.unmodifiableEncodeAdvices();
then(orderedEncodeAdvices.size()).isEqualTo(3);
then(builder.build().clientOptions().unmodifiableEncodeAdvices().size()).isEqualTo(3);
then(builder.build().clientOptions().unmodifiableEncodeAdvices().get(0)).isEqualTo(encodeAdvice2);
}

@Test
Expand All @@ -238,6 +257,8 @@ void testAddInterceptor() {

List<RestInterceptor> orderedInterceptors = builder.unmodifiableInterceptors();
then(orderedInterceptors.size()).isEqualTo(3);
then(builder.build().clientOptions().unmodifiableInterceptors().size()).isEqualTo(3);
then(builder.build().clientOptions().unmodifiableInterceptors().get(0)).isEqualTo(interceptor2);
}

private ByteEncoder createByteEncoder(int order) {
Expand Down
2 changes: 1 addition & 1 deletion site/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ enable = false

# multi-versions
[[params.versions]]
version = "v1.0.0 (Latest)"
version = "v1.0.1 (Latest)"
url = "/docs"
[[params.versions]]
version = "v0.1.0"
Expand Down
1 change: 0 additions & 1 deletion site/content/en/blog/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ weight: 30
description: >
This is the **blog** section. It has categories: Releases.
---
AAA

33 changes: 25 additions & 8 deletions site/content/en/docs/codec/decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ description: >
`RestClient`会自动根据用户的 `Headers` 与 期望`Entity`类型 等选择合适的`Decoder`进行解码。`RestClient`内置了下面这些`Decoder`:
- Json
- jackson(默认)
- fastjson
- gson
- ProtoBuf
- String
- byte[]
- jackson :默认,自动通过SPI的方式注入到RestClient中
- fastjson :需要引入`fastjson`依赖,并将`FastJsonCodec`添加到RestClient中
- gson :需要引入`gson`依赖,并将`GsonCodec`添加到RestClient中
- ProtoBuf :需要引入`ProtoBuf`依赖,并将`ProtoBufCodec`添加到RestClient中
- String :自动通过SPI的方式注入到RestClient中
- byte[] :自动通过SPI的方式注入到RestClient中
除此之外`RestClient`也支持用户自定义解码器。
---
Expand All @@ -33,9 +38,21 @@ Person person = response.bodyToEntity(Person.class);
其中Json相关的序列化方式默认配置了日期格式为`yyyy-MM-dd HH:mm:ss`
{{< /alert >}}
## 使用ProtoBuf Decoder
当Response的`contentType``ProtoBufCodec.PROTO_BUF`,且`response.bodyToEntity()`传入的类型为`com.google.protobuf.Message`的子类时,将自动使用`ProtoBuf Decoder`来进行`Decode`
### Step1 : 引入ProtoBuf依赖
```xml
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
```
### Step2 : 使用 ProtoBuf Decoder 进行解码
将ProtoBufCodec加入到RestClient中, 当Response的`contentType``ProtoBufCodec.PROTO_BUF`,且`response.bodyToEntity()`传入的类型为`com.google.protobuf.Message`的子类时,将自动使用`ProtoBuf Decoder`来进行`Decode`
```java
final RestClient client = RestClient.ofDefault();
//将ProtoBufCodec加入到RestClient中
final RestClient client = RestClient.create()
.addDecoder(new ProtoBufCodec())
.build();

RestResponseBase response = client.get("localhost:8080/aaa")
.execute()
.toCompletableFuture()
Expand Down
40 changes: 30 additions & 10 deletions site/content/en/docs/codec/encoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ weight: 10
description: >
`RestClient`会自动根据用户的 `Headers` 与 `Entity` 等选择合适的`Encoder`进行`Encode`。其内置了下面这些`Encoder`:
- Json
- jackson(默认)
- fastjson
- gson
- ProtoBuf
- File
- String
- byte[]
- Json
- jackson :默认,自动通过SPI的方式注入到RestClient中
- fastjson :需要引入`fastjson`依赖,并将`FastJsonCodec`添加到RestClient中
- gson :需要引入`gson`依赖,并将`GsonCodec`添加到RestClient中
- ProtoBuf :需要引入`ProtoBuf`依赖,并将`ProtoBufCodec`添加到RestClient中
- File :自动通过SPI的方式注入到RestClient中
- String :自动通过SPI的方式注入到RestClient中
- byte[] :自动通过SPI的方式注入到RestClient中
除此之外`RestClient`也支持用户自定义`Encoder`。
---
Expand All @@ -32,9 +39,22 @@ RestResponseBase response = client.post("localhost:8080/aaa")
其中Json相关的序列化方式默认配置了日期格式为`yyyy-MM-dd HH:mm:ss`
{{< /alert >}}
## 使用ProtoBuf Encoder
指定`contentType``ProtoBufCodec.PROTO_BUF`,且`Entity`类型为`com.google.protobuf.Message`的子类时,将自动使用`ProtoBuf Encoder`来对`Entity`来进行`Encode`。示例如下:
### Step1 : 引入ProtoBuf依赖
```xml
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
```

### Step2 : 使用 ProtoBuf Encoder 进行编码
将ProtoBufCodec加入到RestClient中,指定`contentType``ProtoBufCodec.PROTO_BUF`,且`Entity`类型为`com.google.protobuf.Message`的子类时,将自动使用`ProtoBuf Encoder`来对`Entity`来进行`Encode`。示例如下:
```java
final RestClient client = RestClient.ofDefault();
//将ProtoBufCodec加入到RestClient中
final RestClient client = RestClient.create()
.addEncoder(new ProtoBufCodec())
.build();

RestResponseBase response = client.post("localhost:8080/aaa")
.contentType(ProtoBufCodec.PROTO_BUF)
.entity(message)
Expand Down

0 comments on commit 9389a1c

Please sign in to comment.