WebClient
#14
Open
wasiullah.khan
wants to merge 6 commits from WebClient into dev-pending-20-01-2026
@ -1,23 +1,123 @@
|
||||
package com.mfsys.aconnect.configuration.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.WebClientResponseException;
|
||||
|
||||
|
||||
@Configuration
|
||||
@Service
|
||||
public class WebClientConfig {
|
||||
|
||||
private final String baseUrl = "http://localhost:8080/digitalkisaan";
|
||||
private final WebClient webClient = WebClient.builder().build();
|
||||
|
||||
public ResponseEntity<Object> post(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(Object.class)
|
||||
.block();
|
||||
} catch (WebClientResponseException ex){
|
||||
return buildErrorResponse(ex);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
try {
|
||||
return webClient
|
||||
.get()
|
||||
.uri(url)
|
||||
.headers(h -> h.addAll(headers))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.toEntity(Object.class)
|
||||
.block();
|
||||
} catch (WebClientResponseException ex){
|
||||
return buildErrorResponse(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<Object> put(String url, Object body, HttpHeaders headers) {
|
||||
try {
|
||||
return webClient
|
||||
.put()
|
||||
.uri(url)
|
||||
.headers(h -> h.addAll(headers))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(body)
|
||||
.retrieve()
|
||||
.toEntity(Object.class)
|
||||
.block();
|
||||
} catch (WebClientResponseException ex){
|
||||
return buildErrorResponse(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public ResponseEntity<Object> delete(String url, HttpHeaders headers) {
|
||||
try {
|
||||
return webClient
|
||||
.delete()
|
||||
.uri(url)
|
||||
.headers(h -> h.addAll(headers))
|
||||
.accept(MediaType.APPLICATION_JSON)
|
||||
.retrieve()
|
||||
.toEntity(Object.class)
|
||||
.block();
|
||||
} catch (WebClientResponseException ex) {
|
||||
return buildErrorResponse(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private ResponseEntity<Object> buildErrorResponse(WebClientResponseException ex){
|
||||
Object errorBody;
|
||||
try{
|
||||
errorBody = ex.getResponseBodyAs(Object.class);
|
||||
}catch (Exception e) {
|
||||
errorBody = ex.getResponseBodyAsString();
|
||||
}
|
||||
return ResponseEntity
|
||||
.status(ex.getStatusCode())
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.body(errorBody);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebClient webClient(WebClient.Builder builder) {
|
||||
return builder
|
||||
.baseUrl(baseUrl) // Set the base URL for all requests
|
||||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) // Set a default content type
|
||||
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) // Set a default accept header
|
||||
.build();
|
||||
}
|
||||
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…
Reference in New Issue