Merge pull request #11091 from Trixi-Turny/BAEL-5054-upload-pdf-with-webclient
BAEL-5054 2 examples of file upload with webclient
This commit is contained in:
commit
499aec0982
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.reactive.controller;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.reactive.service.ReactiveUploadService;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class UploadController {
|
||||||
|
final ReactiveUploadService uploadService;
|
||||||
|
|
||||||
|
public UploadController(ReactiveUploadService uploadService) {
|
||||||
|
this.uploadService = uploadService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(path = "/upload")
|
||||||
|
@ResponseBody
|
||||||
|
public Mono<HttpStatus> uploadPdf(@RequestParam("file") final MultipartFile multipartFile) {
|
||||||
|
return uploadService.uploadPdf(multipartFile.getResource());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(path = "/upload/multipart")
|
||||||
|
@ResponseBody
|
||||||
|
public Mono<HttpStatus> uploadMultipart(@RequestParam("file") final MultipartFile multipartFile) {
|
||||||
|
return uploadService.uploadMultipart(multipartFile);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.baeldung.reactive.exception;
|
||||||
|
|
||||||
|
public class ServiceException extends RuntimeException{
|
||||||
|
|
||||||
|
public ServiceException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,66 @@
|
|||||||
|
package com.baeldung.reactive.service;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.reactive.exception.ServiceException;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.client.MultipartBodyBuilder;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ReactiveUploadService {
|
||||||
|
|
||||||
|
private final WebClient webClient;
|
||||||
|
private static final String EXTERNAL_UPLOAD_URL = "http://localhost:8080/external/upload";
|
||||||
|
|
||||||
|
public ReactiveUploadService(final WebClient webClient) {
|
||||||
|
this.webClient = webClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Mono<HttpStatus> uploadPdf(final Resource resource) {
|
||||||
|
|
||||||
|
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
|
||||||
|
Mono<HttpStatus> httpStatusMono = webClient.post()
|
||||||
|
.uri(url)
|
||||||
|
.contentType(MediaType.APPLICATION_PDF)
|
||||||
|
.body(BodyInserters.fromResource(resource))
|
||||||
|
.exchangeToMono(response -> {
|
||||||
|
if (response.statusCode().equals(HttpStatus.OK)) {
|
||||||
|
return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
|
||||||
|
} else {
|
||||||
|
throw new ServiceException("Error uploading file");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return httpStatusMono;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Mono<HttpStatus> uploadMultipart(final MultipartFile multipartFile) {
|
||||||
|
final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri();
|
||||||
|
|
||||||
|
final MultipartBodyBuilder builder = new MultipartBodyBuilder();
|
||||||
|
builder.part("file", multipartFile.getResource());
|
||||||
|
|
||||||
|
Mono<HttpStatus> httpStatusMono = webClient.post()
|
||||||
|
.uri(url)
|
||||||
|
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||||
|
.body(BodyInserters.fromMultipartData(builder.build()))
|
||||||
|
.exchangeToMono(response -> {
|
||||||
|
if (response.statusCode().equals(HttpStatus.OK)) {
|
||||||
|
return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode());
|
||||||
|
} else {
|
||||||
|
throw new ServiceException("Error uploading file");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return httpStatusMono;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.baeldung.reactive.service;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
class ReactiveUploadServiceUnitTest {
|
||||||
|
|
||||||
|
private static final String BASE_URL = "http://localhost:8080/external/upload";
|
||||||
|
|
||||||
|
final WebClient webClientMock = WebClient.builder().baseUrl(BASE_URL)
|
||||||
|
.exchangeFunction(clientRequest -> Mono.just(ClientResponse.create(HttpStatus.OK)
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.build()))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
private final ReactiveUploadService tested = new ReactiveUploadService(webClientMock);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAPdf_whenUploadingWithWebClient_thenOK() {
|
||||||
|
final Resource file = mock(Resource.class);
|
||||||
|
|
||||||
|
final Mono<HttpStatus> result = tested.uploadPdf(file);
|
||||||
|
final HttpStatus status = result.block();
|
||||||
|
|
||||||
|
assertThat(status).isEqualTo(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenAMultipartPdf_whenUploadingWithWebClient_thenOK() {
|
||||||
|
final Resource file = mock(Resource.class);
|
||||||
|
final MultipartFile multipartFile = mock(MultipartFile.class);
|
||||||
|
when(multipartFile.getResource()).thenReturn(file);
|
||||||
|
|
||||||
|
final Mono<HttpStatus> result = tested.uploadMultipart(multipartFile);
|
||||||
|
final HttpStatus status = result.block();
|
||||||
|
|
||||||
|
assertThat(status).isEqualTo(HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user