Skip to content

Commit faad2e1

Browse files
HttpEntity with headers
1 parent 47e565f commit faad2e1

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/main/java/com/orange/lo/sdk/rest/ResourceClient.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
import org.springframework.core.ParameterizedTypeReference;
1313
import org.springframework.http.HttpEntity;
14+
import org.springframework.http.HttpHeaders;
1415
import org.springframework.http.HttpMethod;
16+
import org.springframework.http.MediaType;
1517
import org.springframework.http.ResponseEntity;
1618
import org.springframework.web.client.RestTemplate;
1719

@@ -50,15 +52,24 @@ protected void delete(String uri) {
5052
}
5153

5254
protected <T, S> S create(String uri, T body, Class<S> responseType) {
53-
ResponseEntity<S> exchange = restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity<T>(body), responseType);
55+
HttpEntity<T> requestEntity = gettHttpEntity(body);
56+
ResponseEntity<S> exchange = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, responseType);
5457
return exchange.getBody();
5558
}
5659

5760
protected <T> void update(String uri, T body) {
58-
restTemplate.exchange(uri, HttpMethod.PUT, new HttpEntity<T>(body), Void.class);
61+
HttpEntity<T> requestEntity = gettHttpEntity(body);
62+
restTemplate.exchange(uri, HttpMethod.PUT, requestEntity, Void.class);
5963
}
60-
64+
6165
protected <T> void edit(String uri, T body) {
62-
restTemplate.exchange(uri, HttpMethod.PATCH, new HttpEntity<T>(body), Void.class);
66+
HttpEntity<T> requestEntity = gettHttpEntity(body);
67+
restTemplate.exchange(uri, HttpMethod.PATCH, requestEntity, Void.class);
68+
}
69+
70+
private static <T> HttpEntity<T> gettHttpEntity(T body) {
71+
HttpHeaders jsonHeaders = new HttpHeaders();
72+
jsonHeaders.setContentType(MediaType.APPLICATION_JSON);
73+
return new HttpEntity<>(body, jsonHeaders);
6374
}
6475
}

0 commit comments

Comments
 (0)