Updated PendingCRMApplicationsService from Rest Template to Web Client

WebClient
parent 44827ff194
commit a13f5643b9

@ -1,10 +1,10 @@
package com.mfsys.aconnect.client.service;
import com.mfsys.aconnect.client.dto.*;
import com.mfsys.aconnect.configuration.config.WebClientConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
@ -16,10 +16,9 @@ public class PendingCRMApplicationsService {
@Value("${app.crm.uri}")
private String crmURI;
private final RestTemplate restTemplate;
public PendingCRMApplicationsService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
private final WebClientConfig webClientConfig;
public PendingCRMApplicationsService(WebClientConfig webClientConfig) {
this.webClientConfig = webClientConfig;
}
private Map<String, Object> param(String columnName, Object columnValue, String columnType, String conditionType) {
@ -193,13 +192,10 @@ public class PendingCRMApplicationsService {
headers.set("SUS_USERCODE", susUserCode);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<LazyListRequestDTO> entity = new HttpEntity<>(payload, headers);
return restTemplate.exchange(
return webClientConfig.postForString(
url,
HttpMethod.POST,
entity,
String.class
payload,
headers
);
}
@ -216,13 +212,10 @@ public class PendingCRMApplicationsService {
headers.set("SUS_USERCODE", susUserCode);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<LazyListRequestDTO> entity = new HttpEntity<>(payload, headers);
return restTemplate.exchange(
return webClientConfig.postForString(
url,
HttpMethod.POST,
entity,
String.class
payload,
headers
);
}
@ -256,11 +249,10 @@ public class PendingCRMApplicationsService {
headers.set("SUS_USERCODE", susUserCode);
headers.setContentType(MediaType.APPLICATION_JSON);
return restTemplate.exchange(
return webClientConfig.postForString(
url,
HttpMethod.POST,
new HttpEntity<>(dto, headers),
String.class
dto,
headers
);
}
@ -294,11 +286,10 @@ public class PendingCRMApplicationsService {
headers.set("SUS_USERCODE", susUserCode);
headers.setContentType(MediaType.APPLICATION_JSON);
return restTemplate.exchange(
return webClientConfig.postForString(
url,
HttpMethod.POST,
new HttpEntity<>(dto, headers),
String.class
dto,
headers
);
}

@ -32,6 +32,23 @@ public class WebClientConfig {
}
}
public ResponseEntity<String> postForString(String url, Object body, HttpHeaders headers) {
try {
return webClient
.post()
.uri(url)
.headers(h -> h.addAll(headers))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.bodyValue(body)
.retrieve()
.toEntity(String.class)
.block();
} catch (WebClientResponseException ex) {
return buildErrorResponseString(ex);
}
}
public <T> ResponseEntity<T> postMultipart(String url, MultiValueMap<String, Object> body, HttpHeaders headers, ParameterizedTypeReference<T> responseType) {
try {
return webClient
@ -108,6 +125,20 @@ public class WebClientConfig {
.body(errorBody);
}
private ResponseEntity<String> buildErrorResponseString(WebClientResponseException ex) {
String errorBody;
try {
errorBody = ex.getResponseBodyAsString();
} catch (Exception e) {
errorBody = ex.getMessage();
}
return ResponseEntity
.status(ex.getStatusCode())
.contentType(MediaType.APPLICATION_JSON)
.body(errorBody);
}
private <T> ResponseEntity<T> buildErrorResponseTyped(WebClientResponseException ex) {
T errorBody;
try {

Loading…
Cancel
Save