Created
April 19, 2022 06:31
-
-
Save Nikhiladiga/1b07db15156781a5290c62b56252a44f to your computer and use it in GitHub Desktop.
Fully generic method to make synchronous http requests using Spring webclient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.List; | |
import java.util.Map; | |
import org.springframework.core.io.FileSystemResource; | |
import org.springframework.http.HttpEntity; | |
import org.springframework.http.HttpMethod; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.client.MultipartBodyBuilder; | |
import org.springframework.util.MultiValueMap; | |
import org.springframework.web.reactive.function.BodyInserters; | |
import org.springframework.web.reactive.function.client.WebClient; | |
public class CustomWebClient { | |
private final WebClient webClient; | |
private String scheme; | |
private String host; | |
private int port; | |
private String path; | |
public CustomWebClient() { | |
webClient = WebClient.builder().build(); | |
} | |
private MultiValueMap<String, HttpEntity<?>> addFiles(List<File> files) { | |
MultipartBodyBuilder builder = new MultipartBodyBuilder(); | |
if(files != null && files.size() > 0){ | |
for(File file : files){ | |
builder.part("file", new FileSystemResource(file)); | |
} | |
} | |
return builder.build(); | |
} | |
private void getURIParams(String apiUrl) { | |
this.scheme = apiUrl.substring(0, apiUrl.indexOf(":")); | |
this.host = apiUrl.substring(scheme.length() + 3, apiUrl.lastIndexOf(":")); | |
String urlHolder = apiUrl.substring(apiUrl.lastIndexOf(":") + 1); | |
this.port = Integer.parseInt(urlHolder.substring(0, urlHolder.indexOf("/"))); | |
this.path = urlHolder.substring(urlHolder.indexOf("/")); | |
} | |
public <T> T httpRequest( | |
HttpMethod httpMethod, | |
String apiURL, | |
Map<String, String> params, | |
Map<String, String> headers, | |
String requestBody, | |
MediaType mediaType, | |
Class<T> responseClass, | |
List<File> files, | |
long timeout | |
) { | |
return webClient | |
.method(httpMethod) | |
.uri(uriBuilder -> { | |
getURIParams(apiURL); | |
uriBuilder.scheme(scheme).host(host).port(port).path(path); | |
if(params != null){ | |
for (Map.Entry<String, String> entry : params.entrySet()) { | |
uriBuilder.queryParam(entry.getKey(), entry.getValue()); | |
} | |
} | |
return uriBuilder.build(); | |
}) | |
.headers(httpHeaders -> { | |
httpHeaders.set("Content-Type", "application/JSON"); | |
if (headers != null) { | |
for (Map.Entry<String, String> entry : headers.entrySet()) { | |
httpHeaders.set(entry.getKey(), entry.getValue()); | |
} | |
} | |
}) | |
.contentType(mediaType).accept(MediaType.ALL) | |
.body( | |
mediaType.equals(MediaType.APPLICATION_JSON) ? | |
BodyInserters.fromValue(requestBody != null ? requestBody : "") : | |
BodyInserters.fromMultipartData(addFiles(files))) | |
.retrieve() | |
.bodyToMono(responseClass) | |
.timeout(Duration.ofSeconds(timeout)) | |
.onErrorMap(e -> new Exception("Message", e)) | |
.block(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment