Updated UploadDocumentService from Rest Template to Web Client

WebClient
Wasiullah Khan 5 days ago
parent 561663f1ef
commit 44827ff194

@ -1,11 +1,11 @@
package com.mfsys.aconnect.client.service; package com.mfsys.aconnect.client.service;
import com.mfsys.aconnect.configuration.config.WebClientConfig;
import org.springframework.util.*; import org.springframework.util.*;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*; import org.springframework.http.*;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.*; import java.util.*;
@ -19,10 +19,9 @@ public class UploadDocumentService {
@Value("${app.crm.uri}") @Value("${app.crm.uri}")
private String crmURI; private String crmURI;
private final RestTemplate restTemplate; private final WebClientConfig webClientConfig;
public UploadDocumentService(WebClientConfig webClientConfig){
public UploadDocumentService(RestTemplate restTemplate){ this.webClientConfig = webClientConfig;
this.restTemplate = restTemplate;
} }
public ResponseEntity<Map<String, String>> depositCacheFiles( public ResponseEntity<Map<String, String>> depositCacheFiles(
@ -44,12 +43,10 @@ public class UploadDocumentService {
body.add("file", file.getResource()); body.add("file", file.getResource());
} }
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); return webClientConfig.postMultipart(
return restTemplate.exchange(
url, url,
HttpMethod.POST, body,
requestEntity, headers,
new ParameterizedTypeReference<Map<String, String>>() {} new ParameterizedTypeReference<Map<String, String>>() {}
); );
} }
@ -73,12 +70,10 @@ public class UploadDocumentService {
body.add("file", file.getResource()); body.add("file", file.getResource());
} }
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers); return webClientConfig.postMultipart(
return restTemplate.exchange(
url, url,
HttpMethod.POST, body,
requestEntity, headers,
new ParameterizedTypeReference<Map<String, String>>() {} new ParameterizedTypeReference<Map<String, String>>() {}
); );
} }

@ -1,9 +1,12 @@
package com.mfsys.aconnect.configuration.config; package com.mfsys.aconnect.configuration.config;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException; import org.springframework.web.reactive.function.client.WebClientResponseException;
@ -29,6 +32,22 @@ public class WebClientConfig {
} }
} }
public <T> ResponseEntity<T> postMultipart(String url, MultiValueMap<String, Object> body, HttpHeaders headers, ParameterizedTypeReference<T> responseType) {
try {
return webClient
.post()
.uri(url)
.headers(h -> h.addAll(headers))
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(body))
.retrieve()
.toEntity(responseType)
.block();
} catch (WebClientResponseException ex) {
return buildErrorResponseTyped(ex);
}
}
public ResponseEntity<Object> get(String url, HttpHeaders headers) { public ResponseEntity<Object> get(String url, HttpHeaders headers) {
try { try {
return webClient return webClient
@ -88,4 +107,17 @@ public class WebClientConfig {
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.body(errorBody); .body(errorBody);
} }
private <T> ResponseEntity<T> buildErrorResponseTyped(WebClientResponseException ex) {
T errorBody;
try {
errorBody = (T) ex.getResponseBodyAs(Object.class);
} catch (Exception e) {
errorBody = (T) ex.getResponseBodyAsString();
}
return ResponseEntity
.status(ex.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body(errorBody);
}
} }

Loading…
Cancel
Save