Skip to content

Commit 34ee892

Browse files
mstrYodakdavisk6
authored andcommitted
Feature/replace deprecated body (#959)
In this pr the old `body()` method calls replaced with `requestBody().asBytes()` method which both exists in Request class. The intention is to remove deprecated code and keep source code clean. Related to #857 * replaced old body with new Body.asBytes()
1 parent 92d0f64 commit 34ee892

11 files changed

Lines changed: 24 additions & 18 deletions

File tree

core/src/main/java/feign/Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
111111
connection.addRequestProperty("Accept", "*/*");
112112
}
113113

114-
if (request.body() != null) {
114+
if (request.requestBody().asBytes() != null) {
115115
if (contentLength != null) {
116116
connection.setFixedLengthStreamingMode(contentLength);
117117
} else {
@@ -125,7 +125,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
125125
out = new DeflaterOutputStream(out);
126126
}
127127
try {
128-
out.write(request.body());
128+
out.write(request.requestBody().asBytes());
129129
} finally {
130130
try {
131131
out.close();

core/src/main/java/feign/FeignException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static FeignException errorReading(Request request, Response response, IOExcepti
6868
response.status(),
6969
format("%s reading %s %s", cause.getMessage(), request.httpMethod(), request.url()),
7070
cause,
71-
request.body());
71+
request.requestBody().asBytes());
7272
}
7373

7474
public static FeignException errorStatus(String methodKey, Response response) {

core/src/main/java/feign/Logger.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ protected void logRequest(String configKey, Level logLevel, Request request) {
5656
}
5757

5858
int bodyLength = 0;
59-
if (request.body() != null) {
60-
bodyLength = request.body().length;
59+
if (request.requestBody().asBytes() != null) {
60+
bodyLength = request.requestBody().asBytes().length;
6161
if (logLevel.ordinal() >= Level.FULL.ordinal()) {
6262
String bodyText =
63-
request.charset() != null ? new String(request.body(), request.charset()) : null;
63+
request.charset() != null
64+
? new String(request.requestBody().asBytes(), request.charset())
65+
: null;
6466
log(configKey, ""); // CRLF
6567
log(configKey, "%s", bodyText != null ? bodyText : "Binary data");
6668
}

core/src/test/java/feign/TargetTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Request apply(RequestTemplate input) {
6262
urlEncoded.httpMethod(),
6363
urlEncoded.url().replace("%2F", "/"),
6464
urlEncoded.headers(),
65-
urlEncoded.body(),
65+
urlEncoded.requestBody().asBytes(),
6666
urlEncoded.charset());
6767
}
6868
};

httpclient/src/main/java/feign/httpclient/ApacheHttpClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ HttpUriRequest toHttpUriRequest(Request request, Request.Options options)
134134
}
135135

136136
// request body
137-
if (request.body() != null) {
137+
if (request.requestBody().asBytes() != null) {
138138
HttpEntity entity = null;
139139
if (request.charset() != null) {
140140
ContentType contentType = getContentType(request);
141-
String content = new String(request.body(), request.charset());
141+
String content = new String(request.requestBody().asBytes(), request.charset());
142142
entity = new StringEntity(content, contentType);
143143
} else {
144-
entity = new ByteArrayEntity(request.body());
144+
entity = new ByteArrayEntity(request.requestBody().asBytes());
145145
}
146146

147147
requestBuilder.setEntity(entity);

java11/src/main/java/feign/http2client/Http2Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ private Builder newRequestBuilder(Request request) throws IOException {
7979
}
8080

8181
final BodyPublisher body;
82-
if (request.body() == null) {
82+
if (request.requestBody().asBytes() == null) {
8383
body = BodyPublishers.noBody();
8484
} else {
85-
body = BodyPublishers.ofByteArray(request.body());
85+
body = BodyPublishers.ofByteArray(request.requestBody().asBytes());
8686
}
8787

8888
final Builder requestBuilder = HttpRequest.newBuilder().uri(uri).version(Version.HTTP_2);

jaxrs2/src/main/java/feign/jaxrs2/JAXRSClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ public feign.Response execute(feign.Request request, Options options) throws IOE
7171
}
7272

7373
private Entity<byte[]> createRequestEntity(feign.Request request) {
74-
if (request.body() == null) {
74+
if (request.requestBody().asBytes() == null) {
7575
return null;
7676
}
7777

7878
return Entity.entity(
79-
request.body(),
79+
request.requestBody().asBytes(),
8080
new Variant(
8181
mediaType(request.headers()), locale(request.headers()), encoding(request.charset())));
8282
}

mock/src/main/java/feign/mock/RequestKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private RequestKey(Request request) {
112112
this.url = buildUrl(request);
113113
this.headers = RequestHeaders.of(request.headers());
114114
this.charset = request.charset();
115-
this.body = request.body();
115+
this.body = request.requestBody().asBytes();
116116
}
117117

118118
public HttpMethod getMethod() {

mock/src/test/java/feign/mock/MockClientTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ public void verifyInvocation() {
184184
mockClient.verifyTimes(HttpMethod.POST, "/repos/netflix/feign/contributors", 1);
185185
assertThat(results, hasSize(1));
186186

187-
byte[] body = mockClient.verifyOne(HttpMethod.POST, "/repos/netflix/feign/contributors").body();
187+
byte[] body =
188+
mockClient
189+
.verifyOne(HttpMethod.POST, "/repos/netflix/feign/contributors")
190+
.requestBody()
191+
.asBytes();
188192
assertThat(body, notNullValue());
189193

190194
String message = new String(body);

okhttp/src/main/java/feign/okhttp/OkHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static Request toOkHttpRequest(feign.Request input) {
7171
requestBuilder.addHeader("Accept", "*/*");
7272
}
7373

74-
byte[] inputBody = input.body();
74+
byte[] inputBody = input.requestBody().asBytes();
7575
boolean isMethodWithBody =
7676
HttpMethod.POST == input.httpMethod()
7777
|| HttpMethod.PUT == input.httpMethod()

0 commit comments

Comments
 (0)